Share via


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

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

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

メモ GetSystemTime 関数を世界協定時刻 (UTC、グリニッジ平均時とも呼ばれます) を返します。 ローカル時間を取得するには、必要があります追加またはタイム ゾーンと UTC との間の時間数を減算します。 たとえば、24: 00 (午前 0 時) UTC で 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
EndStructureDeclareFunction GetSystemTime Lib"CoreDll.dll" _
    (ByRef lpSystemTime As SYSTEMTIME) As UInt32

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

PublicSub GetTime
    ' Call the native GetSystemTime method    ' with the defined structure.Dim st AsNew SYSTEMTIME
    GetSystemTime(st)

    ' Show the current time.
    MessageBox.Show("Current Time: "  & st.wHour.ToString() _
        & ":" & st.wMinute.ToString())
EndSubPublicSub SetTime
    ' Call the native GetSystemTime method    ' with the defined structure.Dim st AsNew SYSTEMTIME
    GetSystemTime(st)

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

EndSub
[DllImport("coredll.dll")]
privateexternstaticvoid GetSystemTime(ref SYSTEMTIME lpSystemTime);

[DllImport("coredll.dll")]
privateexternstaticuint SetSystemTime(ref SYSTEMTIME lpSystemTime);


privatestruct SYSTEMTIME 
{
    publicushort wYear;
    publicushort wMonth; 
    publicushort wDayOfWeek; 
    publicushort wDay; 
    publicushort wHour; 
    publicushort wMinute; 
    publicushort wSecond; 
    publicushort wMilliseconds; 
}

privatevoid 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());
}
privatevoid 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 の相互運用性