Share via


Comment : obtenir ou définir l'heure système

Mise à jour : novembre 2007

Pour obtenir ou définir l'heure système du périphérique, utilisez l'appel de code non managé pour appeler la fonction GetSystemTime ou SetSystemTime native.

Notez que la fonction GetSystemTime retourne le temps universel (UTC, Coordinated Universal Time), également connu sous le nom d'heure de Greenwich (Greenwich Mean Time). Pour obtenir votre heure locale, vous devez ajouter ou soustraire le nombre d'heures entre votre fuseau horaire et l'heure UTC. Par exemple, 24:00 (minuit) en UTC correspondent à 19:00 à New York, soit un offset de moins 5 heures (UTC-5).

Pour déterminer l'offset UTC de votre fuseau horaire, consultez l'onglet Fuseau horaire dans Propriétés de la date et de l'heure.

Certains émulateurs de périphérique ne définissent pas initialement l'heure d'été correctement, ce qui peut affecter votre résultat.

Exemple

Cet exemple de code définit les éléments suivants :

  • Déclarations d'appel de code non managé pour les méthodes natives dans Windows Embedded CE.

  • Une structure permettant d'échanger avec les méthodes natives.

  • Une méthode managée nommée GetTime qui affiche l'heure actuelle.

  • Une méthode managée nommée SetTime qui avance l'horloge système d'une heure.

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());
}

Compilation du code

Cet exemple nécessite des références aux espaces de noms suivants :

Voir aussi

Autres ressources

Interopérabilité dans le .NET Compact Framework