Share via


Como: Obter ou conjunto a time do sistema

Para obter ou conjunto a time do sistema do dispositivo, use invocação de plataforma para chamar o nativo GetSystemTime ou SetSystemTime funções.

Observe que o GetSystemTime função retorna a hora de universal coordenado (UTC, também conhecido sistema autônomo Greenwich Mean Time). Para obter a hora local, você deve adicionar ou subtrair o número de horas entre a sua zona de tempo e a UTC.Por exemplo, 24:00 (meia-noite) no UTC é 19:00 em Nova York --um deslocamento de menos 5 horas (UTC – 5).

Para determinar o deslocamento UTC para sua zona de tempo, consulte a guia Time Zone das propriedades Date and Time.

Alguns emuladores de dispositivo não inicializam o horário de verão corretamente, o que pode afetar o resultado.

Exemplo

Este exemplo de código define o seguinte:

  • Declarações de invocação de plataforma para os métodos nativo em Windows Embedded CE.

  • Uma estrutura para passar e receber mensagens dos métodos nativos.

  • Um método gerenciado nomeado GetTime, que exibe a time corrente.

  • Um método gerenciado nomeado SetTime, que define o relógio do sistema uma hora a frente.

Public Structure SYSTEMTIME
    Public wYear As UInt16
    Public wMonth As UInt16
    Public wDayOfWeek As UInt16
    Public wDay As UInt16
    Public wHour As UInt16
    Public wMinute As UInt16
    Public wSecond As UInt16
    Public wMilliseconds As UInt16
End Structure

Declare Function GetSystemTime Lib "CoreDll.dll" _
    (ByRef lpSystemTime As SYSTEMTIME) As UInt32

Declare Function SetSystemTime Lib "CoreDll.dll" _
    (ByRef lpSystemTime As SYSTEMTIME) As UInt32

Public Sub GetTime
    ' Call the native GetSystemTime method
    ' with the defined structure.
    Dim st As New SYSTEMTIME
    GetSystemTime(st)

    ' Show the current time.
    MessageBox.Show("Current Time: "  & st.wHour.ToString() _
        & ":" & st.wMinute.ToString())
End Sub

Public Sub SetTime
    ' Call the native GetSystemTime method
    ' with the defined structure.
   Dim st As New SYSTEMTIME
    GetSystemTime(st)

    ' Set the system clock ahead one hour.
    st.wHour = Convert.ToUInt16(((CInt(st.wHour) + 1)) Mod 24)
    SetSystemTime(st)

End Sub



[DllImport("coredll.dll")]
private extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);

[DllImport("coredll.dll")]
private extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);


private struct SYSTEMTIME 
{
    public ushort wYear;
    public ushort wMonth; 
    public ushort wDayOfWeek; 
    public ushort wDay; 
    public ushort wHour; 
    public ushort wMinute; 
    public ushort wSecond; 
    public ushort wMilliseconds; 
}

private void GetTime()
{
    // Call the native GetSystemTime method
    // with the defined structure.
    SYSTEMTIME stime = new SYSTEMTIME();
    GetSystemTime(ref stime);

    // Show the current time.           
    MessageBox.Show("Current Time: "  + 
        stime.wHour.ToString() + ":"
        + stime.wMinute.ToString());
}
private void SetTime()
{
    // Call the native GetSystemTime method
    // with the defined structure.
    SYSTEMTIME systime = new SYSTEMTIME();
    GetSystemTime(ref systime);

    // Set the system clock ahead one hour.
    systime.wHour = (ushort)(systime.wHour + 1 % 24);
    SetSystemTime(ref systime);
    MessageBox.Show("New time: " + systime.wHour.ToString() + ":"
        + systime.wMinute.ToString());
}

Compilando o código

Este exemplo requer referências aos seguintes namespaces:

Consulte também

Outros recursos

Interoperabilidade no .NET Compact Framework