개체를 가져오는 TimeZoneInfo 가장 일반적인 방법은 레지스트리에서 개체에 대한 정보를 검색하는 것입니다. 개체를 가져오려면 static (Visual Basic의 경우 Shared) 메서드를 호출하여 레지스트리를 찾습니다. 메서드에서 throw된 예외를 처리하며, 특히 레지스트리에 표준 시간대가 정의되지 않은 경우 발생하는 TimeZoneNotFoundException 예외를 다룹니다.
메모
.NET 8 TimeZoneInfo.FindSystemTimeZoneById 부터 새 개체를 인스턴스화하는 대신 캐시된 TimeZoneInfo 개체를 반환합니다. 자세한 내용은 FindSystemTimeZoneById가 새 개체를 반환하지 않음을 참조하세요.
예시
다음 코드는 동부 표준 시간대를 나타내는 개체를 검색 TimeZoneInfo 하고 현지 시간에 해당하는 동부 표준시를 표시합니다.
DateTime timeNow = DateTime.Now;
try
{
TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime easternTimeNow = TimeZoneInfo.ConvertTime(
timeNow,
TimeZoneInfo.Local,
easternZone
);
Console.WriteLine("{0} {1} corresponds to {2} {3}.",
timeNow,
TimeZoneInfo.Local.IsDaylightSavingTime(timeNow) ?
TimeZoneInfo.Local.DaylightName :
TimeZoneInfo.Local.StandardName,
easternTimeNow,
easternZone.IsDaylightSavingTime(easternTimeNow) ?
easternZone.DaylightName :
easternZone.StandardName);
}
// Handle exception
//
// As an alternative to simply displaying an error message, an alternate Eastern
// Standard Time TimeZoneInfo object could be instantiated here either by restoring
// it from a serialized string or by providing the necessary data to the
// CreateCustomTimeZone method.
catch (TimeZoneNotFoundException)
{
Console.WriteLine("The Eastern Standard Time Zone cannot be found on the local system.");
}
catch (InvalidTimeZoneException)
{
Console.WriteLine("The Eastern Standard Time Zone contains invalid or missing data.");
}
catch (SecurityException)
{
Console.WriteLine("The application lacks permission to read time zone information from the registry.");
}
catch (OutOfMemoryException)
{
Console.WriteLine("Not enough memory is available to load information on the Eastern Standard Time zone.");
}
// If we weren't passing FindSystemTimeZoneById a literal string, we also
// would handle an ArgumentNullException.
Dim timeNow As Date = Date.Now
Try
Dim easternZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
Dim easternTimeNow As Date = TimeZoneInfo.ConvertTime(timeNow, TimeZoneInfo.Local, easternZone)
Console.WriteLine("{0} {1} corresponds to {2} {3}.", _
timeNow, _
IIf(TimeZoneInfo.Local.IsDaylightSavingTime(timeNow), _
TimeZoneInfo.Local.DaylightName, TimeZoneInfo.Local.StandardName), _
easternTimeNow, _
IIf(easternZone.IsDaylightSavingTime(easternTimeNow), _
easternZone.DaylightName, easternZone.StandardName))
' Handle exception
'
' As an alternative to simply displaying an error message, an alternate Eastern
' Standard Time TimeZoneInfo object could be instantiated here either by restoring
' it from a serialized string or by providing the necessary data to the
' CreateCustomTimeZone method.
Catch e As TimeZoneNotFoundException
Console.WriteLine("The Eastern Standard Time Zone cannot be found on the local system.")
Catch e As InvalidTimeZoneException
Console.WriteLine("The Eastern Standard Time Zone contains invalid or missing data.")
Catch e As SecurityException
Console.WriteLine("The application lacks permission to read time zone information from the registry.")
Catch e As OutOfMemoryException
Console.WriteLine("Not enough memory is available to load information on the Eastern Standard Time zone.")
' If we weren't passing FindSystemTimeZoneById a literal string, we also
' would handle an ArgumentNullException.
End Try
TimeZoneInfo.FindSystemTimeZoneById 메서드의 단일 매개 변수는 검색할 표준 시간대의 식별자이며 이는 개체의 TimeZoneInfo.Id 속성에 해당합니다. 표준 시간대 식별자는 표준 시간대를 고유하게 식별하는 키 필드입니다. 대부분의 키는 비교적 짧지만 표준 시간대 식별자는 비교적 깁니다. 대부분의 경우, 값은 TimeZoneInfo 오브젝트의 StandardName 속성에 해당하며, 이는 표준 시간대의 표준시 이름을 제공하는 데 사용됩니다. 그러나 예외도 있습니다. 유효한 식별자를 제공하는 가장 좋은 방법은 시스템에서 사용할 수 있는 표준 시간대를 열거하고 해당 식별자에 있는 표준 시간대의 식별자를 적어 두는 것입니다. 자세한 내용은 방법: 컴퓨터에 있는 표준 시간대 열거를 참조하세요. 로컬 시스템 문서에 정의된 표준 시간대 찾기에는 선택한 표준 시간대 식별자 목록도 포함되어 있습니다.
표준 시간대를 찾은 경우 메서드는 해당 개체를 반환합니다 TimeZoneInfo . 표준 시간대를 찾지 못하면 메서드가 TimeZoneNotFoundException 예외를 발생시킵니다. 표준 시간대를 찾았지만 데이터가 손상되었거나 불완전한 경우, 메서드는 InvalidTimeZoneException를 던집니다.
애플리케이션이 있어야 하는 표준 시간대를 사용하는 경우 먼저 메서드를 FindSystemTimeZoneById 호출하여 레지스트리에서 표준 시간대 정보를 검색해야 합니다. 메서드 호출이 실패하면 예외 처리기가 표준 시간대의 새 인스턴스를 만들거나 직렬화된 TimeZoneInfo 개체를 역직렬화하여 다시 만들어야 합니다. 포함된 리소스에서 표준 시간대를 복원하는 방법의 예제를 참조하세요.
참고하십시오
.NET