BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 3.5+

Get Time Zone Information

When creating applications that will be used internationally, particularly with a shared database that holds date and time data, it is important to be able to obtain information about time zones. This can be achieved with .NET 3.5's TimeZoneInfo class.

Retrieving a List of Valid Time Zones

In some situations you will wish to obtain the complete list of time zones that are known by the local system. This can be achieved by calling the GetSystemTimeZones method. The method returns a ReadOnlyCollection of TimeZoneInfo objects. To output all known time zones using a foreach loop, execute the code below:

ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();

foreach (TimeZoneInfo zone in zones)
{
    Console.WriteLine("{0}", zone.Id);
}

Time Zone Adjustment Rules

Time zones can include rules that cause the local clock to be adjusted periodically for daylight saving time. The rules for the dates and times for such changes vary around the world and can be different for countries that are within the same geographical time zone. The TimeZoneInfo class can be used to obtain information about these time changes using instances of the nested class, "TimeZoneInfo.AdjustmentRule".

Retrieving Adjustment Rules

To obtain the list of adjustment rules for a time zone, execute the TimeZoneInfo object's GetAdjustmentRules method. This method returns an array of AdjustmentRule objects with five useful properties:

  • DateStart. This property holds a date and time value that specifies the start of the period for which the adjustment rule applies. This date is not related to the start of daylight saving time directly. It allows for multiple rules to be defined, each applying to a different period. This allows for situations where a country changes the rules for daylight saving time.
  • DateEnd. This property holds a date and time value that specifies the end of the period for which the adjustment rule applies.
  • DaylightDelta. This property returns a TimeSpan that determines the amount of time that is added during daylight saving time.
  • DaylightTransitionStart. The DaylightTransitionStart property returns a TransitionTime structure that provides rules for the transition from standard time to daylight saving time, including several properties that can be used to determine the exact date of the transition. This structure is described below.
  • DaylightTransitionEnd. This property returns a TransitionTime structure that defines the end of daylight saving time and the return to standard time.

The first three of the above properties are the simplest as they return single values. For Greenwich Mean Time (GMT), we can view the properties by executing the following sample code. This shows that the single adjustment rule is active for the entire range of dates that the .NET framework understands. The offset for this period is an increase of one hour.

TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
TimeZoneInfo.AdjustmentRule[] rules = tz.GetAdjustmentRules();
TimeZoneInfo.AdjustmentRule rule = rules[0];

Console.WriteLine(rule.DateStart);
Console.WriteLine(rule.DateEnd);
Console.WriteLine(rule.DaylightDelta);

/* OUTPUT

01/01/0001 00:00:00
31/12/9999 00:00:00
01:00:00

*/

TransitionTime Structure

The DaylightTransitionStart and DaylightTransitionEnd properties of the AdjustmentRule class define the rules for calculating the start and end of the daylight saving time period. These rules vary between countries so several properties are required. In addition, there are two manners in which a transition time can be defined. These are known as "fixed date" and "floating date". You can determine which type of rule is in use by reading the Boolean IsFixedDateRule property.

Fixed Dates

A transition with a fixed date occurs on the same date of every year. If the IsFixedDateRule property returns true, you can determine this date by reading the Day and Month properties, each of which returns an integer value. To determine the time of the transition, read the TimeOfDay property. This returns a DateTime value with the correct time set. The date portion of the TimeOfDay property should not be used.

Floating Dates

A floating date rule is required where transitions can occur on a different date from one year to the next. An example of such a rule is employed in the UK, where the transition from GMT to British Summer Time (BST) occurs at 1:00am on the last Sunday of March. To provide enough flexibility to define floating date rules, four properties are provided:

  • Month. The Month property returns an integer value that specifies the number of the month in which the transition occurs. A value of one represents January.
  • Week. The Week property returns an integer specifying the week number for the transition. A value of between one and four specifies the first, second, third or fourth week of the month. A value of five specifies that the transition occurs in the last week of the month.
  • DayOfWeek. This property returns a value from the DayOfWeek enumeration. It determines upon which day of the week the transition occurs.
  • TimeOfDay. As with the fixed date rules, the TimeOfDay property is a DateTime value with the time element representing the time of the transition.

The following shows the rules that define the transition from GMT to BST:

TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
TimeZoneInfo.AdjustmentRule[] rules = tz.GetAdjustmentRules();
TimeZoneInfo.AdjustmentRule rule = rules[0];
TimeZoneInfo.TransitionTime start = rule.DaylightTransitionStart;

Console.WriteLine("Month\t{0}", start.Month);
Console.WriteLine("Week\t{0}", start.Week);
Console.WriteLine("Day\t{0}", start.DayOfWeek);
Console.WriteLine("Time\t{0}", start.TimeOfDay.ToShortTimeString());

/* OUTPUT

Month   3
Week    5
Day     Sunday
Time    01:00

*/
12 October 2009