如何:获取 TimeZoneInfo 对象

获取 TimeZoneInfo 对象的常用方法是从注册表中检索与其有关的信息。 若要获取该对象,请调用 static(Visual Basic 中的 SharedTimeZoneInfo.FindSystemTimeZoneById 方法,该方法在注册表中进行查找。 处理方法引发的所有异常,尤其是注册表中未定义时区时引发的 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 属性。 时区标识符是唯一标识时区的键字段。 虽然大多数键都相对较短,但时区标识符相对较长。 在大多数情况下,其值对应于 StandardName 对象的 TimeZoneInfo 属性,该属性用于提供时区标准时间的名称。 但是,有例外情况。 确保提供有效标识符的最好方法是枚举系统上可用的时区,然后记下上面显示的时区标识符。 有关说明,请参阅 How to: Enumerate time zones present on a computer查找本地系统中定义的时区一文还包含所选时区标识符的列表。

如果找到时区,该方法将返回其 TimeZoneInfo 对象。 如果未找到时区,该方法将引发 TimeZoneNotFoundException。 如果找到该时区但其数据已损坏或不完整,该方法将引发 InvalidTimeZoneException

如果你的应用程序依赖于一个必须存在的时区,则应首先调用 FindSystemTimeZoneById 方法从注册表中检索时区信息。 如果方法调用失败,则异常处理程序应创建时区的新实例,或通过对序列化的 TimeZoneInfo 对象进行反序列化来重新创建它。 请参阅如何:从嵌入的资源还原时区以获取示例。

另请参阅