Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetSystemTimeZones missing GMT Standard Time

I have been trying to use TimeZoneInfo.GetSystemTimeZones in .net4 to replace our old method which uses COM to call into some MFC code. However, I've had to revert to the COM method because GetSystemTimeZones doesn't return all time zones. Being based in the UK and not including GMT Standard Time in our list of options is a problem.

When I look in the registry, I find that GMT Standard Time has a Dynamic DST subkey with no rules defined. I downloaded the .net4 source code and, in TimeZoneInfo.cs, GetSystemTimeZones eventually calls TryCreateAdjustmentRules which appears to return false if the Dynamic DST key has no FirstEntry or LastEntry defined.

            //
            // loop over all of the "<time_zone_name>\Dynamic DST" hive entries 
            // 
            // read FirstEntry  {MinValue      - (year1, 12, 31)}
            // read MiddleEntry {(yearN, 1, 1) - (yearN, 12, 31)} 
            // read LastEntry   {(yearN, 1, 1) - MaxValue       }

            // read the FirstEntry and LastEntry key values (ex: "1980", "2038")
            Int32 first = (Int32)dynamicKey.GetValue(c_firstEntryValue, -1, RegistryValueOptions.None); 
            Int32 last = (Int32)dynamicKey.GetValue(c_lastEntryValue, -1, RegistryValueOptions.None);

            if (first == -1 || last == -1 || first > last) { 
                rules = null;
                return false; 
            }

As TryCreateAdjustmentRules returns false, TryGetTimeZoneByRegistryKey throws an InvalidTimeZoneException which means GMT Standard Time doesn't get added into the time zone collection. I've tried deleting the empty Dynamic DST subkeys but something adds them back in again (probably Windows Update).

I've done alot of searching but haven't seen anyone else report this problem. I'm not sure whether there is a GetSystemTimeZones fix or whether I can stop the empty Dynamic DST subkey appearing in the first place. Any ideas?

like image 437
Richard Hawkins Avatar asked Mar 09 '12 15:03

Richard Hawkins


2 Answers

I'm guessing this is just a registry issue.

Something similar is outlined here for WinXP / Server 2003 SP1. Please see if the following helps: http://blogs.technet.com/b/dst2007/archive/2007/03/06/missing-time-zones-in-registry.aspx

My guess is you could export the entire registry Key from another machine, and apply it to your problematic box(es).

like image 100
JFish222 Avatar answered Sep 18 '22 11:09

JFish222


It turns out that the problem is with our COM object and not with the .NET code. The code to read the registry should only be reading but it mistakenly calls RegCreateKey to optimise multiple calls to the same registry branch.

When the COM object is registered (with admin rights) because of a new version, it loads the time zones and creates Dynamic DST for the local time zone which in my case is GMT/UTC.

Administrators, I guess this question should be marked for deletion.

like image 31
Richard Hawkins Avatar answered Sep 19 '22 11:09

Richard Hawkins