
Time Zones and the TimeZoneInfo Class
In the .NET Framework, a TimeZoneInfo object represents a time zone. The 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. (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. The IsFixedDateRule property indicates whether that transition is fixed or floating.
The .NET Framework 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. For example, in Windows XP the registry contains data about only a single set of time zone adjustments. 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. 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.
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. 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. It should also provide some method of letting the application to continue if a required TimeZoneInfo object cannot be instantiated from the registry.
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. 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). 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.
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. For this purpose, the TimeZoneInfo object includes several conversion methods, including:
ConvertTimeFromUtc, which converts UTC to the time in a designated time zone.
ConvertTimeToUtc, which converts the time in a designated time zone to UTC.
ConvertTime, which converts the time in one designated time zone to the time in another designated time zone.
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.