HOW TO:具現化 TimeZoneInfo 物件
更新:2007 年 11 月
將 TimeZoneInfo 物件具現化最常使用的方法,就是從登錄中擷取相關資訊。本主題討論如何從本機系統登錄中將 TimeZoneInfo 物件具現化。
具現化 TimeZoneInfo 物件
宣告 TimeZoneInfo 物件。
呼叫 static (在 Visual Basic 中為 Shared) TimeZoneInfo.FindSystemTimeZoneById 方法。
處理方法所擲回的任何例外狀況,特別是當登錄中未定義時區時所擲回的 TimeZoneNotFoundException。
範例
下列程式碼會擷取代表「東部標準時間」區的 TimeZoneInfo 物件,並顯示對應於當地時間的「東部標準時間」。
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
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.
TimeZoneInfo.FindSystemTimeZoneById 方法的單一參數是您要擷取的時區之識別項,對應於物件的 TimeZoneInfo.Id 屬性。時區識別項是唯一識別時區的索引鍵欄位。大部分的索引鍵都很短,相較之下時區識別項就很長。大部分的情況下,識別項的值對應於 TimeZoneInfo 物件的 StandardName 屬性,用來提供時區的標準時間名稱。但也有例外。要確保您提供的是有效的識別項最好的方式,就是列舉系統中可用的時區並記錄時區中顯示的識別項。如需說明,請參閱 HOW TO:列舉電腦上展示的時區。尋找定義於本機系統的時區主題也包含所選取時區識別項的清單。
如果找到了時區,方法就會傳回它的 TimeZoneInfo 物件。如果找不到時區,方法就會擲回 TimeZoneNotFoundException。如果找到時區但資料已經損壞或不完整,方法就會擲回 InvalidTimeZoneException。
如果您的應用程式依賴必須存在的時區,就應該先呼叫 FindSystemTimeZoneById 方法,從登錄中擷取時區資訊。如果方法呼叫失敗,您的例外處理常式應該就會建立時區的新執行個體,或將已序列化的 TimeZoneInfo 物件還原序列化。如需範例,請參閱 HOW TO:從內嵌資源還原時區。
編譯程式碼
這個範例需要:
將 System.Core.dll 的參考加入至專案中。
以 using 陳述式 (C# 程式碼中的必要項) 匯入 System 命名空間。