방법: 미리 정의된 UTC 및 현지 표준 시간대 개체에 액세스
TimeZoneInfo 클래스에서는 두 가지 속성 즉, 미리 정의된 표준 시간대 개체에 대한 코드 액세스 권한을 부여하는 Utc 및 Local을 제공합니다. 이 항목에서는 이러한 속성에서 반환되는 TimeZoneInfo 개체에 액세스하는 방법에 대해 설명합니다.
UTC(협정 세계시) TimeZoneInfo 개체에 액세스하려면
static(Visual Basic의 경우 Shared) TimeZoneInfo.Utc 속성을 사용하여 협정 세계시에 액세스합니다.
이 속성에서 반환되는 TimeZoneInfo 개체를 개체 변수에 할당하지 않고 계속해서 TimeZoneInfo.Utc 속성을 통해 협정 세계시에 액세스합니다.
현지 표준 시간대에 액세스하려면
static(Visual Basic의 경우 Shared) TimeZoneInfo.Local 속성을 사용하여 로컬 시스템의 표준 시간대에 액세스합니다.
이 속성에서 반환되는 TimeZoneInfo 개체를 개체 변수에 할당하지 않고 계속해서 TimeZoneInfo.Local 속성을 통해 현지 표준 시간대에 액세스합니다.
예제
다음 코드에서는 TimeZoneInfo.Local 및 TimeZoneInfo.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
// 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 {0} zone cannot be found in the registry.",
timeZoneName);
}
catch (InvalidTimeZoneException)
{
Console.WriteLine("The registry contains invalid data for the {0} zone.",
timeZoneName);
}
현지 표준 시간대를 TimeZoneInfo 개체 변수에 할당하지 않고 항상 TimeZoneInfo.Local 속성을 통해 현지 표준 시간대에 액세스해야 합니다. 마찬가지로 UTC 시간대를 TimeZoneInfo 개체 변수에 할당하지 않고 항상 TimeZoneInfo.Utc 속성을 통해 협정 세계시에 액세스해야 합니다. 이렇게 하면 TimeZoneInfo 개체 변수가 TimeZoneInfo.ClearCachedData 메서드에 대한 호출로 무효화되지 않습니다.
코드 컴파일
이 예제에는 다음 사항이 필요합니다.
System.Core.dll에 대한 참조를 프로젝트에 추가해야 합니다.
System 네임스페이스를 using 문을 사용하여 가져와야 합니다(C# 코드인 경우 필요).