방법: 시스템 시간 가져오기 또는 설정

업데이트: 2007년 11월

장치의 시스템 시간을 가져오거나 설정하려면 플랫폼 호출을 사용하여 네이티브 함수 GetSystemTime 또는 SetSystemTime을 호출합니다.

GetSystemTime 함수는 협정 세계시(UTC, 그리니치 표준시라고도 함)를 반환합니다. 현지 시간을 가져오려면 표준 시간대 및 UTC 간에 시간을 더하거나 빼야 합니다. 예를 들어, UTC로 24:00(자정)는 뉴욕의 경우 오프셋 5시간을 뺀(UTC–5) 19:00입니다.

표준 시간대에 대한 UTC 오프셋을 결정하려면 날짜 및 시간 속성의 표준 시간대 탭을 확인합니다.

일부 장치 에뮬레이터는 처음에 일광 절약 시간이 올바르게 설정되어 있지 않으므로 결과에 영향을 줄 수 있습니다.

예제

이 코드 예제에서는 다음을 정의합니다.

  • Windows Embedded CE에서 네이티브 메서드에 대한 플랫폼 호출 선언

  • 네이티브 메서드에 전달하거나 네이티브 메서드에서 받을 구조체

  • 현재 시간을 표시하는 관리되는 메서드(GetTime)

  • 시스템 시계를 한 시간 빠르게 설정하는 관리되는 메서드(SetTime)

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의 상호 운용성