次の方法で共有


方法 : システム時刻を取得または設定する

更新 : 2007 年 11 月

デバイスのシステム時刻を取得または設定するには、ネイティブの GetSystemTime または SetSystemTime 関数を呼び出すプラットフォーム呼び出しを使用します。

GetSystemTime 関数が世界協定時刻 (UTC、グリニッジ標準時とも呼ばれています) を返すことに注意してください。現地時刻を取得するには、タイム ゾーンと UTC の間で相違する時間数を加算または減算する必要があります。たとえば、UTC の 24:00 (午前零時) はニューヨークの 19:00 であり、その差はマイナス 5 時間 (UTC-5) です。

タイム ゾーンと UTC の時間差を特定するには、[日付と時刻のプロパティ] の [タイム ゾーン] タブを参照します。

一部のデバイス エミュレータでは夏時間が正しく初期設定されておらず、作業結果に影響する場合があります。

使用例

このコード例では、次の各項目を定義します。

  • Windows Embedded CE のネイティブなメソッドのプラットフォーム呼び出しの宣言。

  • ネイティブなメソッドとやり取りする構造体。

  • GetTime という名前のマネージ メソッド。現在の時刻を表示します。

  • SetTime という名前のマネージ メソッド。システム時計を 1 時間先に進めます。

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

コードのコンパイル方法

この例は、次の名前空間への参照を必要とします。

参照

その他の技術情報

.NET Compact Framework の相互運用性