BlackWaspTM
.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.

TimeZoneInfo Class

Version 3.5 of the .NET framework introduced a new class named "TimeZoneInfo". Objects of this class provide properties that describe a time zone and methods that can be used to process date and time information in a time zone-specific manner. In this article we will examine how to instantiate a TimeZoneInfo object and interpret its properties.

Retrieving Local Time Zone Information

The TimeZoneInfo class does not provide a public constructor. In order to obtain an instance, you must either read the value of a static property or execute a static method. The first of these that we will use is the Local property. This returns a TimeZoneInfo object that represents the user's local time zone, incorporating the daylight savings time settings that have been applied.

The following code outputs a name for the local time zone to the console. The result will vary according to your computer's configuration:

Console.WriteLine(TimeZoneInfo.Local.DisplayName);

NB: You should always access the properties of the local time zone using the Local property and not assign the values to a variable. This avoids the risk that the variable will become invalid due to changes to the underlying object, specifically by calls to the ClearCachedData method.

TimeZoneInfo Properties

The TimeZoneInfo class provides several properties that describe the time zone. These are:

  • Id. Every time zone has a unique identifier string. This ID can be used when retrieving a TimeZoneInfo object.
  • DisplayName. This property returns a localised display name for the time zone.
  • StandardName. The StandardName property returns a localised display name that describes the time zone during normal times; ie. not during daylight saving time.
  • DaylightName. The DaylightName property returns a string that describes the time zone during daylight saving time periods.
  • BaseUtcOffset. This property returns a TimeSpan structure that represents the time zone's offset from Coordinated Universal Time (UTC). It does not include additional changes due to daylight saving time. A positive value indicates that the local time is ahead of UTC.
  • SupportsDaylightSavingTime. This Boolean property returns true if the time zone supports daylight saving time changes, or false if it does not.

The code below shows the values of the properties for the local time zone for a user in the UK. Results for other time zones may vary.

Console.WriteLine("ID\t{0}", TimeZoneInfo.Local.Id);
Console.WriteLine("Name\t{0}", TimeZoneInfo.Local.DisplayName);
Console.WriteLine("S. Name\t{0}", TimeZoneInfo.Local.StandardName);
Console.WriteLine("D. Name\t{0}", TimeZoneInfo.Local.DaylightName);
Console.WriteLine("Offset\t{0}", TimeZoneInfo.Local.BaseUtcOffset);
Console.WriteLine("DST?\t{0}", TimeZoneInfo.Local.SupportsDaylightSavingTime);

/* OUTPUT

ID      GMT Standard Time
Name    (UTC) Dublin, Edinburgh, Lisbon, London
S. Name GMT Standard Time
D. Name GMT Daylight Time
Offset  00:00:00
DST?    True

*/

Retrieving UTC Time Zone Information

When storing date and time information in an international application, UTC times are very important. One common strategy is to store all times using UTC and only converting to a local time for display purposes or reporting. This reduces the problems of comparing times in different time zones and the risk of ambiguous times that occur near the transition periods between standard and daylight saving time.

The TimeZoneInfo class includes a static property that represents a special UTC time zone. This time zone does not support daylight saving time, so has no ambiguous times, and always has an offset of zero. To access the UTC time zone information, use the static Utc property:

Console.WriteLine("ID\t{0}", TimeZoneInfo.Utc.Id);
Console.WriteLine("Name\t{0}", TimeZoneInfo.Utc.DisplayName);
Console.WriteLine("S. Name\t{0}", TimeZoneInfo.Utc.StandardName);
Console.WriteLine("D. Name\t{0}", TimeZoneInfo.Utc.DaylightName);
Console.WriteLine("Offset\t{0}", TimeZoneInfo.Utc.BaseUtcOffset);
Console.WriteLine("DST?\t{0}", TimeZoneInfo.Utc.SupportsDaylightSavingTime);

/* OUTPUT

ID      UTC
Name    UTC
S. Name UTC
D. Name UTC
Offset  00:00:00
DST?    False

*/

Retrieving a Specific Time Zone

If you wish to obtain information about a time zone that is neither the local time zone nor UTC, you can access it using the FindSystemTimeZoneById method, passing the ID of the desired time zone as the only parameter. This method is also useful if you wish to retrieve the local time zone of the user but ignore any changes they may have made to the daylight saving time setting.

TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
12 October 2009