While recently working on the live tile implementation for my Windows Phone 7 hobby project InTheKnow, I had a need to implement Unix Time Zone support using a TZ database. This list of Time Zones is widely used by Unix systems around the world as their source of Time Zone information. Sadly Microsoft’s .Net framework doesn’t have any support for this library – but like other parts of the framework that developers have found to be lacking, there is a library out there to fill the gap.
A bit of background
My Windows Phone 7 app InTheKnow provides a way for users to review their Google Analytics statistics on their Windows Phone 7 device. Because it uses Google’s Analytics API’s, and Google uses mostly Linux installations on their servers, all Time Zone information is usually communicated by their API using the ZoneInfo/TZ database naming convention of CountryName/CityName.
Users of my app are located around the world in many different time zones, however my server is located in the US, so when trying to reliably retrieve statistics for display on a users live tile, i needed to know what the date was in the users’ native time zone.
Australia/Sydney
You can see a full list of these Time Zones here.
This works out great if you are using PHP, Ruby of other languages more commonly used on UNIX based systems, as these have this format baked into their core, however Microsoft’s .Net framework has no way of translating these strings into time zones using the framework.
See a need fill a need
Lucky for me the solution to my Time Zone dilemma was stupidly simple as Mark Rodriguez has written a cool little .Net library called ZoneInfo that can be found over on CodePlex.
Mark’s library is great as it takes a standard folder full of ZoneInfo/TZ database files as it’s data source, allowing you to easily update your application with the latest time zone information whether Mark’s project gets maintained in the future or not. This library it deadly simple and basically does exactly what you’d want it to do and nothing more. Job well done i must say, thanks Mark!
Show me the code
The first thing you’ll need to do is head on over to CodePlex and download Mark’s library;
Next you’ll need to download a copy of the latest ZoneInfo/TZ database files. These are provided in lots of places on the interwebs, although i got mine from here:
ftp://elsie.nci.nih.gov/pub/ (download the tzdataXXXXX gzip file and you’re good to go.)
Now you’ll want to add Mark’s ZoneInfo project to your application’s solution, and then add a reference to it in the project you want to call it from;
The next step is to place your TZ database files inside your project somewhere. Extract all the files from your tzdata gzip you downloaded above into a new folder inside your project called TimeZones (you can call this anything, but for the ease of this demo, we’ll just call it TimeZones).
Now, where ever you want to get the time, you can simply do the following:
// load the database files by giving a path to the folder they // are stored in i.e. C:\WhereIKeepMyTZFiles Database.LoadFiles(System.IO.Path.GetFullPath(@"\TimeZones")); // get the time zone in the South Pole, so we can check when // it's OK to call some Japanese Whaling Ships for a chat ;-) Zone zone = Database.GetZone("Antarctica/South_Pole"); // load the current local system time, and convert it // to Universal time DateTime utcTime = DateTime.Now.ToUniversalTime(); // Pull out the time at the South Pole DateTime southernPolarTime = zone.ConvertToLocal(utcTime);
It’s simply that easy!