Overview

Library to manage different time zone and daylight saving time.
The framework 3.5 has introduce a new class System.TimeZoneInfo to manage timezone, it's easy to use but if you don't have all timezone loaded in the system you have to :
  • update the registry with the specified timezone and call the FindSystemTimeZoneById function (not really easy on client/production computer if you are not administrator)
  • create your own custom timezone from your personal database (not easy to maintain) but necessary if you want to keep the history of daylight saving time.

However it's seems that this class don't care about history of oldest daylight saving time but only the current year rules.

Inspired by the Olson database and the PublicDomain project, that is a new c# library to play with time zone with a minimum of maintain (refresh the olson database when needed).

Release

  • 1.1.0.0
Update of tz database with tzdata2011n.
This year (2011) there are an interesting change, at midnight 29 Dec, Samoa changes from UTC-11 to UTC+13 (lost 24 hours). Can be verify with the following code :
TzTimeZone samoa = TzTimeInfo.GetZone("Pacific/Apia");
DateTime utc = new DateTime(2011, 12, 29, 0, 0, 0, DateTimeKind.Utc);
Console.WriteLine("UTC \t\t\t| Samoa Local Time");
for (int i = 1; i <= 48; i++) {
   utc = utc.AddHours(1);
   Console.WriteLine(utc + "\t| " + samoa.ToLocalTime(utc));
}
Output :
UTC                    | Samoa Local Time
30/12/2011 08:00     | 29/12/2011 22:00
30/12/2011 09:00     | 29/12/2011 23:00
30/12/2011 10:00     | 31/12/2011 00:00

Database

By default the library is linked with an olson database files. You can override it by more recent files, for this set the environment variable TZDIR with the new directory of database files.

How to use it

Local time zone can be retrieve by the static TzTimeZone.CurrentTzTimeZone member. The following code uses the CurrentTzTimeZone to return the local times that correspond to UTC.
   TzTimeZone local = TzTimeZone.CurrentTzTimeZone;
   Console.WriteLine(local.ToLocalTime(DateTime.UtcNow));

Specific time zone are initialized by the static TzTimeInfo.GetZone function and translation are provide by ToLocalTime and ToUniversalTime functions.
   DateTime dateLocal = new DateTime(2011, 1, 5, 10, 0, 0, DateTimeKind.Local);
   TzTimeZone paris = TzTimeInfo.GetZone("Europe/Paris");
   TzTimeZone ny = TzTimeInfo.GetZone("America/New_York");
   DateTime dateUtc = paris.ToUniversalTime(dateLocal);
   Console.WriteLine("Local time at paris : " + dateLocal + " UTC : " + dateUtc);
   Console.WriteLine("Local time at new york : " + ny.ToLocalTime(dateUtc));
   Console.WriteLine("");
   Console.WriteLine("Paris : " + dateLocal + " New york " + paris.ToTimeZone(dateLocal, ny));

This example generates the following output :
Local time at paris : 05/01/2011 10:00:00 UTC : 05/01/2011 09:00:00
Local time at new york : 05/01/2011 04:00:00
Paris : 05/01/2011 10:00:00 New york : 05/01/2011 04:00:00

External links

Last edited Sep 14, 2013 at 6:55 AM by LNT, version 15