時區概觀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. 例如,在三月第三個星期日從標準時間到日光節約時間的轉換會遵循浮動調整規則。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:00and 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:00and 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:00and 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. 例如,在三月第三個星期日從標準時間到日光節約時間的轉換會遵循浮動調整規則。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:00and 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. 它也應該提供一些方法,讓應用程式在必要的物件無法從登錄具現化時繼續進行 TimeZoneInfoIt 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