• Home >> Net C# >>  Localisation
  • zoneinfo (tz database / olson database) .net api


  • Rating : Excellent[0]   Very Good[0]   Average[0]   Below Average[0]   Poor[0]
  • Pub Date: 10/30/2008
  • admin [ View Profile ]

  • Abstract
  • This article describes a simple .NET API for using the ZoneInfo database, which is also known as the tz database or Olson database.The API is available at http://www.codeplex.com/zoneinfo.I hope you find it useful :) Background I have been developing a web site which schedules meetings and events across multiple time zones for more than a year now. I previously used the .NET PublicDomain library discussed in another codeproject article......

  • Introduction
  •  Sponsored Links
  • introduction

    this article describes a simple .net api for using the zoneinfo database, which is also known as the tz database or olson database.

    the api is available at http://www.codeplex.com/zoneinfo.

    i hope you find it useful :)

    background

    i have been developing a web site which schedules meetings and events across multiple time zones for more than a year now.

    i previously used the .net publicdomain library discussed in another codeproject article but had a number of issues including:

    • just not working for australia
    • it seems to hard code in the tz database rather than reading it from the file system; therefore if you need to update the database you need to wait for the source code to be updated
    • the library seems to have a lot of other things in it which i just don't need (which is not a major issue though)

    one of the big things i wanted was to have an api which ad the latest tz database and off i go.

    i am not a big fan of reinventing the wheel so if this library just isn't needed please let me know :)

    using the code

    talking a fairly agile approach to this i have just implemented the apis which i have needed.

    the interface itself is extremely simple and hopefully is well illustrated by the examples below:

    loading the data

    // load all the files
    database.loadfiles(@"c:\src\trunk\timezonetest\tzdata");
    

    before you start to use any of the functions, you need to make sure that the database of files are loaded.

    right now .tab files in the timeinfo distribution are not loaded but they are needed for the current set of apis.

    getting the local time somewhere else

    // iterate through all the zone strings
    foreach (string zonename in database.getzonenames())
    {
        zone z = database.getzone(zonename);
    
        console.writeline("zone: " + z.name
                            + "\n\thas a local date time of "
                            + z.now.tostring()
                            + "\n\twhich is a gmt offset of "
                            + z.getutcoffset(z.now).tostring());
    }
    

    the sample above gets all the time zones names and then gets the corresponding zoneinfo.zone instance.

    from this instance you get do a number of things like getting the local time in that zone and its current utc offset.

    zone: europe/malta
            has a local date time of 8/04/2008 9:40:52 am
            which is a gmt offset of 02:00:00
    zone: europe/minsk
            has a local date time of 8/04/2008 10:40:52 am
            which is a gmt offset of 03:00:00
    zone: europe/monaco
            has a local date time of 8/04/2008 9:40:52 am
            which is a gmt offset of 02:00:00
    zone: europe/moscow
            has a local date time of 8/04/2008 11:40:52 am
            which is a gmt offset of 04:00:00 
    

    convert times between time zones

    // what is the time in sao paulo when it is new years day in melbourne?
    zone melbtz = database.getzone("australia/melbourne");
    zone sptz = database.getzone("america/sao_paulo");
    
    datetime melbtime = new datetime(2009, 1, 1, 0, 0, 0, datetimekind.local);
    datetime utctime = melbtz.converttoutc(melbtime);
    datetime sptime = sptz.converttolocal(utctime);
    
    console.writeline("melbourne " + melbtime.tostring()
                      + "\nsao paulo " + sptime.tostring()
                      + "\nutc " + utctime.tostring());
    

    one of the most useful features of the api is the ability to convert arbitrary dates between time zones. a lot of timezone apis only really know what the utc offset is right now, not 6 months from now.

    melbourne 1/01/2009 12:00:00 am
    sao paulo 31/12/2008 11:00:00 am
    utc 31/12/2008 1:00:00 pm
    

    offset cut-over windows

    // what are all the timezone cutover windows for melbourne in the last few years?
    list<datetime> dates =
             melbtz.getcutoverwindows(
                         new datetime(2007, 1, 1, 0, 0, 0, datetimekind.local),
                         new datetime(2009, 12, 31, 23, 59, 59, datetimekind.local));
    
    console.writeline("printing cutovers between 1/1/2007 - 31/12/2009");
    foreach (datetime dt in dates)
    {
         console.writeline("\t" + dt.tostring());
    }
    

    this functionality may seem useless ... until you actually need it.

    if you store dates in the database in utc and need to convert them to the local time of the user you need to apply a utc offset. what happens if the day light offset changes within this window? well, you can know if this is the case rather than assuming that it doesn't matter.

    printing cutovers between 1/1/2007 - 31/12/2009
            30/03/2007 2:00:00 am
            27/10/2007 2:00:00 am
            6/04/2008 2:00:00 am
            5/10/2008 2:00:00 am
            5/04/2009 2:00:00 am
            4/10/2009 2:00:00 am  

    points of interest

    the code seems to work well for australia and the other zones i have tested this with. i am sure as more people use it in different scenarios it will continue to mature.

    one thing i did notice was just how un-standard the format of the tz database was with respect to tab and space delimiting of fields ... the code has a number of hacks unfortunately to compensate this.

    history

    there are still a number of things to be done including:

    - handling of link entries in the files. the current format allows for backward compatibility in the old names. this is not hard to do and will happen soon (or if someone needs it).

    more information

    check out the latest version of the code at http://www.codeplex.com/zoneinfo.

    if you have any issues please log it there and i will look at any changes which need to get done.

  • RATE THIS ARTICLE :
  •  
    • Latest Comments:
      • Add a comment
      • Title:
      • Comment
      •  
    Other popular Localisation articles:
    • localizing windows forms programmatically using a vs2005 add-in

      I wanted to localize my Windows Forms programmatically, and had this problem for several months, and I couldn't find a possible solution in any of the forums. By simply creating a Visual Studio add-in, you can easily make your Windows forms localizable. And here, I publish the basic code for doing t

    • zoneinfo (tz database / olson database) .net api

      This article describes a simple .NET API for using the ZoneInfo database, which is also known as the tz database or Olson database.The API is available at http://www.codeplex.com/zoneinfo.I hope you find it useful :) Background I have been developing a web site which schedules meetings and events ac

    • developing an asp.net page with masterpage and localization

      While seeking on the internet for a solution to implement localization within an ASP.NET application using a MasterPage, I realized that a lot of people have got the same problem to solve. Unfortunately, I could not find a suitable solution thus, I intended to do my own implementation.BackgroundThe

    • introduction to software translation for mfc

      Recently, I have been involved in localization of software applications for global markets. Although software localization and translation is usually (and hopefully) less complex and less expensive than the original development of the application, it is still a complex issue, and it can be difficult

    • survival guide to do resource localization using compact framework 2.0

      The last days I finished a small pocket PC clock application to use when I am working in another city (I am boring to wake up in morning with the windows sound). The application was OK, and a friend said me, why not I localize the application and sales it? The localization was not easy for me, becau

    • multiple language support for mfc applications with extension dll

      I have read a lot of good articles on CodeProject. Now it's time for me to provide one. Please leave any comment or suggestion you may have. All are welcome and greatly appreciated. I do want to get your feedback on this implementation of multiple language support. Following requirements have been m

    • selectively copy resources between binary (exe/dll) files

      This program demonstrates how you can selectively copy some resources from other binary (EXE/DLL) files into a target binary (EXE/DLL) file. The best place it can fit in might be in application localization (if the localization is based on resource modification). If you have other nice usages, pleas

    • resource dlls and language selection menu

      In today's world, localization and translation of software has become an important feature since it dramatically helps in boosting sales. As far as Win32/MFC applications are concerned, managing different languages for your app requires the use of satellite DLLs.This article describes an easy-to-use

    • globalization resources for multilanguage development

      This article contains MUST HAVE tables and data for proper multi language development.I highly recommend to use ISO (International Organizations for Standardization) as primary source of information, because this is the only way to go for high quality products and compatibility. Please remember that

    • windows setthreadlocale and crt setlocale

      Some countries and languages standardize on number and date formats that don't translate smoothly between cultures. It is important for C++/Windows developers to have strategies and techniques to handle this challenge and other challenges presented by diverging sets of localization API functions. Th

    About Us |Contact us |Site Map |Csharp |Visual C / C++ |Visual basic |Java |SQL |Linux / Unix |Ajax
    ©2007-2018 CodeCoolest.com. Ptolive.cn Asp.net source code All Rights Reserved.