如何:实例化 TimeZoneInfo 对象

实例化 TimeZoneInfo 对象的最常见方法是从注册表中检索有关该对象的信息。 本主题讨论如何从本地系统注册表中实例化 TimeZoneInfo 对象。

实例化 TimeZoneInfo 对象

  1. 声明一个 TimeZoneInfo 对象。

  2. 调用 static(在 Visual Basic 中为 Shared)TimeZoneInfo.FindSystemTimeZoneById 方法。

  3. 处理由该方法引发的任何异常,特别是注册表中未定义该时区时引发的 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 属性相对应,该属性用于提供时区的标准时间的名称。 但是,也存在例外情况。 若要确保提供了有效的标识符,最好的方法是枚举系统上的可用时区,并记下表示每个时区的标识符。 有关阐述,请参见如何:枚举计算机上存在的时区查找在本地系统上定义的时区主题还包含选定的时区标识符的列表。

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

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

编译代码

此示例需要:

  • 在项目中添加一个对 System.Core.dll 的引用。

  • 使用 using 语句导入 System 命名空间(在 C# 代码中需要)。

请参见

任务

如何:访问预定义的 UTC 和本地时区对象

概念

查找在本地系统上定义的时区

其他资源

日期、时间和时区