0
Reply

TimeZone registry key always give the same Bias, stardbias, daylightbias

feng

feng

Oct 2 2008 7:14 PM
6.9k

I want to set the windows timezone.

First I read the regitry to get all timezone info.  Here is the code:

 

         public static void GetTimeZones()
        {

            //open key where all time zones are located in the registry

            RegistryKey timeZoneKeys = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones");                      

            //iterate through each time zone in the registry and add it to the hash table

            foreach (string zonekey in timeZoneKeys.GetSubKeyNames())
            {

                //get current time zone key

                RegistryKey individualZone = timeZoneKeys.OpenSubKey(zonekey);

                //create new TZI struct and populate it with values from key

                TimeZoneInformation TZI = new TimeZoneInformation();

                TZI.standardName = individualZone.GetValue("Std").ToString();              

                TZI.daylightName = individualZone.GetValue("Dlt").ToString();

                //read binary TZI data, convert to byte array

                byte[] byTZI = (byte[])individualZone.GetValue("TZI");

                REG_TIME_ZONE_INFORMATION reg_TZI;
                GCHandle h = GCHandle.Alloc(byTZI, GCHandleType.Pinned);
                try
                {
                    reg_TZI = (REG_TIME_ZONE_INFORMATION)Marshal.PtrToStructure(h.AddrOfPinnedObject(),typeof(REG_TIME_ZONE_INFORMATION));
                    TZI.bias = reg_TZI.nBias;
                    TZI.standardBias = reg_TZI.nStandardBias;
                    TZI.daylightBias = reg_TZI.nDaylightBias;
                    TZI.standardDate.wYear = reg_TZI.dtStandardDate.wYear;
                    TZI.standardDate.wMonth = reg_TZI.dtStandardDate.wMonth;
                    TZI.standardDate.wDayOfWeek = reg_TZI.dtStandardDate.wDayOfWeek;
                    TZI.standardDate.wDay = reg_TZI.dtStandardDate.wDay;
                    TZI.standardDate.wHour = reg_TZI.dtStandardDate.wHour;
                    TZI.standardDate.wMinute = reg_TZI.dtStandardDate.wMinute;
                    TZI.standardDate.wSecond = reg_TZI.dtStandardDate.wSecond;
                    TZI.standardDate.wMilliseconds = reg_TZI.dtStandardDate.wMilliseconds;

                    TZI.daylightDate.wYear = reg_TZI.dtDaylightDate.wYear;
                    TZI.daylightDate.wMonth = reg_TZI.dtDaylightDate.wMonth;
                    TZI.daylightDate.wDayOfWeek = reg_TZI.dtDaylightDate.wDayOfWeek;
                    TZI.daylightDate.wDay = reg_TZI.dtDaylightDate.wDay;
                    TZI.daylightDate.wHour = reg_TZI.dtDaylightDate.wHour;
                    TZI.daylightDate.wMinute = reg_TZI.dtDaylightDate.wMinute;
                    TZI.daylightDate.wSecond = reg_TZI.dtDaylightDate.wSecond;
                    TZI.daylightDate.wMilliseconds = reg_TZI.dtDaylightDate.wMilliseconds;

                }
                catch
                { }
                finally
                {
                    h.Free();
                }

               
                //add the name and TZI struct to hash table

                TimeZoneCollection.Add(TZI.standardName, TZI);

            }        

        }

 

       public static void Main()
        {

            TimeZoneTest.TimeZoneInformation tzi = new TimeZoneTest.TimeZoneInformation();
            TimeZoneTest.GetTimeZones();

            if (TimeZoneTest.TimeZoneCollection.ContainsKey("Mountain Standard Time"))
            {
                Console.WriteLine("get the Mountain time!");
                tzi = (TimeZoneTest.TimeZoneInformation)TimeZoneTest.TimeZoneCollection["Mountain Standard Time"];
            }
            Console.WriteLine("the bias of Montain time is {0}, {1}, {2}", tzi.bias, tzi.standardBias, tzi.daylightBias);

            bool result = TimeZoneTest.SetTimeZone(tzi);      

}

The bias for any time zones are same, and the settimezone which calls SetTimeZoneInformation() in kernal32.dll always return false .

 

Any idea,  many thanks