时区概述Time zone overview

TimeZoneInfo类可简化时区感知应用程序的创建过程。The TimeZoneInfo class simplifies the creation of time zone-aware applications. TimeZone类支持使用本地时区和协调世界时 (UTC) 。The TimeZone class supports working with the local time zone and Coordinated Universal Time (UTC). TimeZoneInfo该类同时支持这两个区域,以及在注册表中预定义了哪些信息的任何时区。The TimeZoneInfo class supports both of these zones as well as any time zone about which information is predefined in the registry. 你还可以使用 TimeZoneInfo 来定义系统不具有其相关信息的自定义时区。You can also use TimeZoneInfo to define custom time zones that the system has no information about.

时区概要Time zone essentials

时区指使用相同时间的某个地理区域。A time zone is a geographical region in which the same time is used. 相邻时区通常(但不总是)相差一小时。Typically, but not always, adjacent time zones are one hour apart. 世界上任一时区的时间都能以与协调世界时 (UTC) 之间的时差表示。The time in any of the world's time zones can be expressed as an offset from Coordinated Universal Time (UTC).

世界上许多时区都实施夏令时。Many of the world's time zones support daylight saving time. 夏令时是指,在春季或初夏将时间提前一小时并在夏末或秋季将时间调回正常(或标准)时间,从而最大化地利用光照。Daylight saving time tries to maximize daylight hours by advancing the time forward by one hour in the spring or early summer, and returning to the normal (or standard) time in the late summer or fall. 这种针对标准时间的来回更改就称为调整规则。These changes to and from standard time are known as adjustment rules.

特定时区中针对夏令时的来回转换可通过固定或浮动调整规则进行定义。The transition to and from daylight saving time in a particular time zone can be defined either by a fixed or a floating adjustment rule. 固定调整规则设定一个特定日期,每年都会在此时进行夏令时来回转换。A fixed adjustment rule sets a particular date on which the transition to or from daylight saving time occurs each year. 例如,每年 10 月 25 日从夏令时转换到标准时间,这一案例就是遵循的固定调整规则。For example, a transition from daylight saving time to standard time that occurs each year on October 25 follows a fixed adjustment rule. 浮动调整规则更为常见,该规则会设定在特定月特定星期的某一天进行夏令时转换。Much more common are floating adjustment rules, which set a particular day of a particular week of a particular month for the transition to or from daylight saving time. 例如,在 3 月的第三个星期日从标准时间转换到夏令时,这一案例遵循的就是浮动调整规则。For example, a transition from standard time to daylight saving time that occurs on the third Sunday of March follows a floating adjustment rule.

对于实施调整规则的时区而言,夏令时的来回转换会导致两种反常时间:无效时间和不明确时间。For time zones that support adjustment rules, the transition to and from daylight saving time creates two kinds of anomalous times: invalid times and ambiguous times. 无效时间是指,从标准时间转换到夏令时而产生的不存在的时间。An invalid time is a nonexistent time created by the transition from standard time to daylight saving time. 例如,如果此转换发生在特定日期的凌晨 2:00,For example, if this transition occurs on a particular day at 2:00 A.M. 导致时间变为凌晨 3:00,则每次凌晨 2:00 and causes the time to change to 3:00 A.M., each time interval between 2:00 A.M. 与凌晨 2:59:99 and 2:59:99 A.M. 之间的时间间隔无效。is invalid. 不明确时间是指一个时区内可映射到两个不同时间的时间。An ambiguous time is a time that can be mapped to two different times in a single time zone. 从夏令时转换到标准时间后,便会产生不明确时间。It is created by the transition from daylight saving time to standard time. 例如,如果此转换发生在特定日期的凌晨 2:00,For example, if this transition occurs on a particular day at 2:00 A.M. 导致时间变为凌晨 1:00,则每次凌晨 1:00 and causes the time to change to 1:00 A.M., each time interval between 1:00 A.M. 与凌晨 1:59:99 and 1:59:99 A.M. 之间的时间间隔既可理解为标准时间,也可以是夏令时时间。can be interpreted as either a standard time or a daylight saving time.

时区术语Time zone terminology

下表定义了使用时区和开发时区感知应用程序时常用的术语。The following table defines terms commonly used when working with time zones and developing time zone-aware applications.

术语Term 定义Definition
调整规则Adjustment rule 用于定义何时从标准时间转换为夏令时,以及何时从夏令时转换回标准时间的规则。A rule that defines when the transition from standard time to daylight saving time and back from daylight saving time to standard time occurs. 每个调整规则都有一个开始和结束日期,用于定义规则的位置 (例如调整规则是从1986年1月1日到12月31日,2006) ,增量 (标准时间随着调整规则) 应用程序而更改的时间,以及有关转换在调整期间发生的特定日期和时间的信息。Each adjustment rule has a start and end date that defines when the rule is in place (for example, the adjustment rule is in place from January 1, 1986, to December 31, 2006), a delta (the amount of time by which the standard time changes as a result of the application of the adjustment rule), and information about the specific date and time that the transitions are to occur during the adjustment period. 转换可遵循固定规则或浮动规则。Transitions can follow either a fixed rule or a floating rule.
歧义时间Ambiguous time 一个时区内可映射到两个不同时间的时间。A time that can be mapped to two different times in a single time zone. 在向后调整时钟时间时,例如从时区的夏令时调整到标准时间这段转换期间,便会出现不明确时间。It occurs when the clock time is adjusted back in time, such as during the transition from a time zone's daylight saving time to its standard time. 例如,如果此转换发生在特定日期的凌晨 2:00,For example, if this transition occurs on a particular day at 2:00 A.M. 导致时间变为凌晨 1:00,则每次凌晨 1:00 and causes the time to change to 1:00 A.M., each time interval between 1:00 A.M. 与凌晨 1:59:99 and 1:59:99 A.M. 之间的时间间隔既可理解为标准时间,也可以是夏令时时间。can be interpreted as either a standard time or a daylight saving time.
固定规则Fixed rule 该调整规则设定发生夏令时来回转换的特定日期。An adjustment rule that sets a particular date for the transition to or from daylight saving time. 例如,每年 10 月 25 日从夏令时转换到标准时间,这一案例就是遵循的固定调整规则。For example, a transition from daylight saving time to standard time that occurs each year on October 25 follows a fixed adjustment rule.
浮动规则Floating rule 该规则设定在特定月特定星期的某一天发生夏令时来回转换。An adjustment rule that sets a particular day of a particular week of a particular month for the transition to or from daylight saving time. 例如,在 3 月的第三个星期日从标准时间转换到夏令时,这一案例遵循的就是浮动调整规则。For example, a transition from standard time to daylight saving time that occurs on the third Sunday of March follows a floating adjustment rule.
无效时间Invalid time 从标准时间转换到夏令时而导致产生的不存在的时间。A nonexistent time that is an artifact of the transition from standard time to daylight saving time. 在向前调整时钟时间时,例如从时区的标准时间转换到夏令时这段转换期间,便会出现无效时间。It occurs when the clock time is adjusted forward in time, such as during the transition from a time zone's standard time to its daylight saving time. 例如,如果此转换发生在特定日期的凌晨 2:00,For example, if this transition occurs on a particular day at 2:00 A.M. 导致时间变为凌晨 3:00,则每次凌晨 2:00 and causes the time to change to 3:00 A.M., each time interval between 2:00 A.M. 与凌晨 2:59:99 and 2:59:99 A.M. 之间的时间间隔无效。is invalid.
转换时间Transition time 有关某一特定时区中特定时间更改(例如从夏令时更改为标准时间,或者从标准时间更改为夏令时)的信息。Information about a specific time change, such as the change from daylight saving time to standard time or vice versa, in a particular time zone.

时区和 TimeZoneInfo 类Time zones and the TimeZoneInfo class

在 .NET 中, TimeZoneInfo 对象表示时区。In .NET, a TimeZoneInfo object represents a time zone. TimeZoneInfo类包含 GetAdjustmentRules 方法,该方法返回对象的数组 TimeZoneInfo.AdjustmentRuleThe TimeZoneInfo class includes a GetAdjustmentRules method that returns an array of TimeZoneInfo.AdjustmentRule objects. 此数组的每个元素都提供有关特定时间段内与夏令时之间来回转换的信息。Each element of this array provides information about the transition to and from daylight saving time for a particular time period. (对于不支持夏令时的时区,该方法将返回一个空数组。 ) 每个 TimeZoneInfo.AdjustmentRule 对象都有一个 DaylightTransitionStart 和一个 DaylightTransitionEnd 属性,该属性定义与夏令时之间来回转换的特定日期和时间。(For time zones that do not support daylight saving time, the method returns an empty array.) Each TimeZoneInfo.AdjustmentRule object has a DaylightTransitionStart and a DaylightTransitionEnd property that defines the particular date and time of the transition to and from daylight saving time. IsFixedDateRule属性指示该转换是固定的还是浮动的。The IsFixedDateRule property indicates whether that transition is fixed or floating.

.NET 依赖于 Windows 操作系统提供的时区信息,并将其存储在注册表中。.NET relies on time zone information provided by the Windows operating system and stored in the registry. 由于地球的时区数,并非所有现有时区都在注册表中表示。Due to the number of the earth's time zones, not all existing time zones are represented in the registry. 此外,因为注册表是动态结构,所以可以在其中添加或删除预定义的时区。In addition, because the registry is a dynamic structure, predefined time zones can be added to or removed from it. 最后,注册表不一定包含历史时区数据。Finally, the registry does not necessarily contain historic time zone data. 例如,在 Windows XP 中,注册表只包含有关一组时区调整的数据。For example, in Windows XP the registry contains data about only a single set of time zone adjustments. Windows Vista 支持动态时区数据,这意味着单个时区可以有多个适用于特定年间隔的调整规则。Windows Vista supports dynamic time zone data, which means that a single time zone can have multiple adjustment rules that apply to specific intervals of years. 但是,在 Windows Vista 注册表和支持夏令时中定义的大多数时区只有一个或两个预定义的调整规则。However, most time zones that are defined in the Windows Vista registry and support daylight saving time have only one or two predefined adjustment rules.

在注册表中,类依赖于时区 TimeZoneInfo 感知应用程序无法确定是否在注册表中定义了特定时区。The dependence of the TimeZoneInfo class on the registry means that a time zone-aware application cannot be certain that a particular time zone is defined in the registry. 因此,尝试实例化某个特定时区(除了本地时区或表示 UTC 的时区)时,应使用异常处理。As a result, the attempt to instantiate a specific time zone (other than the local time zone or the time zone that represents UTC) should use exception handling. 它还应提供一些方法,使应用程序在 TimeZoneInfo 无法从注册表中实例化所需的对象时继续运行。It should also provide some method of letting the application to continue if a required TimeZoneInfo object cannot be instantiated from the registry.

为了处理缺少所需的时区,此 TimeZoneInfo 类包含 CreateCustomTimeZone 方法,可用于创建在注册表中找不到的自定义时区。To handle the absence of a required time zone, the TimeZoneInfo class includes a CreateCustomTimeZone method, which you can use to create custom time zones that are not found in the registry. 有关创建自定义时区的详细信息,请参阅 如何:创建不带调整规则的时区如何:创建带有调整规则的时区For details on creating a custom time zone, see How to: Create time zones without adjustment rules and How to: Create time zones with adjustment rules. 此外,还可以使用 ToSerializedString 方法将新创建的时区转换为字符串,并将其保存在数据存储区 (例如数据库、文本文件、注册表或应用程序资源) 中。In addition, you can use the ToSerializedString method to convert a newly created time zone to a string and save it in a data store (such as a database, a text file, the registry, or an application resource). 然后,可以使用 FromSerializedString 方法将此字符串转换回 TimeZoneInfo 对象。You can then use the FromSerializedString method to convert this string back to a TimeZoneInfo object. 有关详细信息,请参阅 如何:将时区保存到嵌入的资源如何:从嵌入的资源还原时区For details, see How to: Save time zones to an embedded resource and How to: Restore time zones from an embedded resource.

由于每个时区均由与 UTC 之间的基本时差表示,同时也可由与反映任何现有调整规则的 UTC 之间的时差表示,因此,可轻易将一个时区内的时间转换为另一时区的时间。Because each time zone is characterized by a base offset from UTC, as well as by an offset from UTC that reflects any existing adjustment rules, a time in one time zone can be easily converted to the time in another time zone. 出于此目的, TimeZoneInfo 对象包括多种转换方法,包括:For this purpose, the TimeZoneInfo object includes several conversion methods, including:

  • ConvertTimeFromUtc,它将 UTC 转换为指定时区中的时间。ConvertTimeFromUtc, which converts UTC to the time in a designated time zone.

  • ConvertTimeToUtc,它将指定时区中的时间转换为 UTC。ConvertTimeToUtc, which converts the time in a designated time zone to UTC.

  • ConvertTime,它将一个指定时区中的时间转换为另一个指定时区中的时间。ConvertTime, which converts the time in one designated time zone to the time in another designated time zone.

  • ConvertTimeBySystemTimeZoneId使用时区标识符 (而不是 TimeZoneInfo 对象) 作为参数,以将一个指定时区中的时间转换为另一个指定时区中的时间。ConvertTimeBySystemTimeZoneId, which uses time zone identifiers (instead of TimeZoneInfo objects) as parameters to convert the time in one designated time zone to the time in another designated time zone.

有关如何在时区之间转换时间的详细信息,请参阅在不同时区间转换时间For details on converting times between time zones, see Converting times between time zones.

另请参阅See also