Wednesday, January 25, 2012

Using ZoneInfo api in wp7

Over recent months I have found planning convenient times for skype meetings between myself and my business partner (who is on the other side of the world) to be a pain. I have been travelling quite a bit so my timezone is constantly changing. I thought "there really should be an app that helps with this"!  I already have world clock for wp7 but this just tells me what the time is now.  I wanted to be able to work out something along the lines of "at 9am tomorrow here in Estonia what will the time be in Brisbane Australia?". 

After a cursory search of the marketplace I couldn't find an app that did this so decided to build the capability into a unit converter app that  I was working on. 

Having worked with timezones in .net before I thought "this will be dead easy". I dived in and built the ui.  Next step was to populate the UI with all of the world time zones so the user can choose which time zone they want to convert from and which time zone they want to convert to.

Unfortunately I soon realised that this would not be as easy as I thought.  In wp7 the TimeZoneInfo class does not have the getsystemtimezones() method. As mentioned in this post.  I'm not sure why? maybe it is because they are trying to keep the wp7 api as lightweight as possible.
Fortunately after some more searching I found http://zoneinfo.codeplex.com/.  A brilliant little .net api for using the tz database of world timezones.

Unfortunately it can't be used with wp7 out of the box, but after a few small modifications it worked great.

I Just did the following:
  1. Downloaded the source code from http://zoneinfo.codeplex.com/
  2. The library is not accessible via silverlight so I needed to create a new silverlight library and copy the classes from the zoneinfo library into it.
  3. I also had to copy the tz database files into the silverlight library and set their build action to resource.
  4. The loadfile / loadfiles methods in Database.cs had to to be altered so that I could pass the uri's of the tz database files.
  5. Lastly I needed to alter the loadfile method to read the file uri and create a StreamReader object.
That's was it.  Now I can convert timezones in wp7.
    //convert local time to the time in Brisbane / Australia
   Zone theZone = Database.GetZone("Australia/Brisbane");
   DateTime d = theZone.ConvertToLocal(DateTime.Now.ToUniversalTime());

1 comment:

  1. Hey, This seems a pretty cool work. Is it possible to have that library?
    Thanks in advance

    ReplyDelete