Tech Off Thread

3 posts

Timezones in Windows

Back to Forum: Tech Off
  • User profile image
    Stitch 2.0

    Hi guys,

    we're have been relying on the Timezone information inside the Windows registry pretty heavily.
    To identify the time zones, we have used the index that is stored in the registry.

    However, we have now found several problems with that:

    • In Windows Vista, the index no longer exists and we couldn't find any definite way to identify each zone.
    • Depending on which version of Windows (Server and Client) our application is running on, there are different time zones in the registry. Some zones only exist in XP, but not on the server.

    When searching the internet for a secure way to rely on the info, always the same piece of code shows up.
    This is how we have used this piece of code so far:

    ArrayList zones = new ArrayList();
    using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones"))
    {
       string[] zoneNames = key.GetSubKeyNames();
       foreach(string zoneName in zoneNames)
       {
          using(RegistryKey subKey = key.OpenSubKey(zoneName))
          {
             try
             {
                TimeZoneInformation tzi = new TimeZoneInformation();
                tzi.name = zoneName;
                tzi.displayName = (
    string)subKey.GetValue("Display");
                tzi.standardName = (
    string)subKey.GetValue("Std");
                tzi.daylightName = (
    string)subKey.GetValue("Dlt");
                tzi.index =
    Convert.ToInt32(subKey.GetValue("Index").ToString());
                tzi.initTzi((
    byte[])subKey.GetValue("Tzi"));
                zones.Add(tzi);
             }
             catch (Exception ex) { }
          }
       }
    }

    TimeZoneInformation[] availableZones = new TimeZoneInformation[zones.Count];
    zones.CopyTo(availableZones);
    Array.Sort(availableZones);

    However, this piece of code won't work on Vista or Server 2003 Enterprise...

    Any ideas what info one should rely on when using Timezones?

    Thanks,
    Stitch

  • User profile image
    Adrian​JMartin
  • User profile image
    Adrian​JMartin

    Theres some demo code on msdn at


    http://msdn2.microsoft.com/en-us/netframework/aa569267.aspx


    Converting Time Zones

    To get it to work on Vista, I just comented out the line of code that queried the registry for the time zone index( TimeZoneIndex.cs Line:145)

    And I had to tweak couple of strings in the TestTimeZone method because it was very specific on its search names.


Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.