Sunday, November 14, 2004

The Code Project - World Clock and the TimeZoneInformation class - .NET

The Code Project - World Clock and the TimeZoneInformation class - .NET

"...This article is an attempt to provide code that is useful for converting a time stamp into the end-user's own time zone.

Another group of users also need to deal with time zones: travellers wishing to know the time at their destination, or the time at home - for example, when trying to call relatives or colleagues. The accompanying application, a demonstration of the TimeZoneInformation class, is useful here.

Different countries and locations around the world use different time zones - to match the time shown on a clock with the approximate local time observed. The simplest description is as an offset from UTC - Universal Time (Coordinated) - although often erroneously described as an offset from GMT - Greenwich Mean Time. GMT can describe the time zone used in Britain, or a specific offset (UTC+0). The issue is complicated by Daylight Savings Time; some locations observe Daylight Savings whereas others do not. Even when they do, they do not agree on the dates and times at which the changes to and from Daylight Savings occur.

Keeping an accurate record of the time zones used around the world is a hard task. Local administrations make rules about the local offset from UTC, and about whether to observe Daylight Savings, and if so, when. Fortunately, Windows has a database of time zone information installed on every system. Unfortunately, Microsoft failed to provide an API for querying this database.

In addition, Windows provides APIs for discovering the currently selected time zone, for converting from UTC to a specified time zone's local time, and (Windows XP and Server 2003 only) for converting from local time, in a specified zone, to UTC.


.NET's Base Class Library offers the System.TimeZone class. This class offers information about the current time zone. However, it does not offer any information about other time zones - what their names are, their offsets, or their daylight savings rules. This class is abstract, so could be extended. I have not yet done so as some features (GetDaylightChanges, IsDaylightSavingTime) will be difficult to implement, while others (ToUniversalTime) can be implemented with OS support, but only on newer operating systems...."


I'll need this ...

I already have a VB6 Time Zone API wrapper class, but when we move to all .Net I would like to replace it with a better implementation.

No comments: