방법: 미리 정의된 UTC 및 현지 표준 시간대 개체에 액세스

TimeZoneInfo 클래스에서는 미리 정의된 시간대 개체에 코드 액세스를 제공하는 두 가지 속성(UtcLocal)을 제공합니다. 이 항목에서는 이러한 속성들이 반환하는 TimeZoneInfo 개체에 액세스하는 방법에 설명합니다.

UTC TimeZoneInfo 개체에 액세스하려면

  1. static(Visual Basic의 경우 Shared) TimeZoneInfo.Utc 속성을 사용하여 협정 세계시에 액세스합니다.

  2. 속성에서 반환하는 TimeZoneInfo 개체를 개체 변수에 할당하는 대신 TimeZoneInfo.Utc 속성을 통해 협정 세계시에 계속 액세스합니다.

현지 표준 시간대 TimeZoneInfo 개체에 액세스하려면

  1. static(Visual Basic의 경우 Shared) TimeZoneInfo.Local 속성을 사용하여 로컬 시스템 표준 시간대에 액세스합니다.

  2. 속성에서 반환하는 TimeZoneInfo 개체를 개체 변수에 할당하는 대신 TimeZoneInfo.Local 속성을 통해 로컬 표준 시간대에 계속 액세스합니다.

예시

다음 코드에서는 TimeZoneInfo.LocalTimeZoneInfo.Utc 속성을 사용하여 시간을 미국 및 캐나다 동부 표준 시간대에서 변환하고 해당 표준 시간대 이름을 콘솔에 표시합니다.

// Create Eastern Standard Time value and TimeZoneInfo object
DateTime estTime = new DateTime(2007, 1, 1, 00, 00, 00);
string timeZoneName = "Eastern Standard Time";
try
{
    TimeZoneInfo est = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName);

    // Convert EST to local time
    DateTime localTime = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Local);
    Console.WriteLine("At {0} {1}, the local time is {2} {3}.",
            estTime,
            est,
            localTime,
            TimeZoneInfo.Local.IsDaylightSavingTime(localTime) ?
                      TimeZoneInfo.Local.DaylightName :
                      TimeZoneInfo.Local.StandardName);

    // Convert EST to UTC
    DateTime utcTime = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Utc);
    Console.WriteLine("At {0} {1}, the time is {2} {3}.",
            estTime,
            est,
            utcTime,
            TimeZoneInfo.Utc.StandardName);
}
catch (TimeZoneNotFoundException)
{
    Console.WriteLine("The {timeZoneName} zone cannot be found in the registry.");
}
catch (InvalidTimeZoneException)
{
    Console.WriteLine("The registry contains invalid data for the {timeZoneName} zone.");
}

// The example produces the following output to the console:
//    At 1/1/2007 12:00:00 AM (UTC-05:00) Eastern Time (US & Canada), the local time is 1/1/2007 12:00:00 AM Eastern Standard Time.
//    At 1/1/2007 12:00:00 AM (UTC-05:00) Eastern Time (US & Canada), the time is 1/1/2007 5:00:00 AM UTC.

' Create Eastern Standard Time value and TimeZoneInfo object      
Dim estTime As Date = #01/01/2007 00:00:00#
Dim timeZoneName As String = "Eastern Standard Time"
Try
    Dim est As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName)

    ' Convert EST to local time
    Dim localTime As Date = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Local)
    Console.WriteLine("At {0} {1}, the local time is {2} {3}.", _
            estTime, _
            est, _
            localTime, _
            IIf(TimeZoneInfo.Local.IsDaylightSavingTime(localTime), _
                TimeZoneInfo.Local.DaylightName, _
                TimeZoneInfo.Local.StandardName))

    ' Convert EST to UTC
    Dim utcTime As Date = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Utc)
    Console.WriteLine("At {0} {1}, the time is {2} {3}.", _
            estTime, _
            est, _
            utcTime, _
            TimeZoneInfo.Utc.StandardName)
Catch e As TimeZoneNotFoundException
    Console.WriteLine("The {0} zone cannot be found in the registry.", _
                      timeZoneName)
Catch e As InvalidTimeZoneException
    Console.WriteLine("The registry contains invalid data for the {0} zone.", _
                      timeZoneName)
End Try

로컬 표준 시간대를 TimeZoneInfo 개체 변수에 할당하는 대신 항상 TimeZoneInfo.Local 속성을 통해 로컬 표준 시간대에 액세스해야 합니다. 마찬가지로 UTC 영역을 TimeZoneInfo 개체 변수에 할당하는 대신 항상 TimeZoneInfo.Utc 속성을 통해 협정 세계시에 액세스해야 합니다. 이렇게 하면 TimeZoneInfo.ClearCachedData 메서드를 호출하여 TimeZoneInfo 개체 변수가 무효화되는 것을 방지할 수 있습니다.

코드 컴파일

이 예제에는 다음 사항이 필요합니다.

  • using 문(C# 코드에 필요)을 사용하여 System 네임스페이스를 가져옵니다.

참고 항목