Problem found when converting time from UTC to local time in certain time zones - by Thomas Mittet 2

Status : 

  Won't Fix<br /><br />
		Due to several factors the product team decided to focus its efforts on other items.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


1
0
Sign in
to vote
ID 778827 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 2/8/2013 5:17:25 AM
Access Restriction Public

Description

I've found a bug in TimeZoneInfo that affects the following methods:
 - ConvertTimeFromUtc
 - ConvertTimeBySystemTimeZoneId (when converting from UTC to "local")
 - GetUtcOffset (when supplied DateTime is of kind UTC)

The symptom is that you get the wrong local time when converting from UTC using either the ConvertTimeFromUtc or the ConvertTimeBySystemTimeZoneId methods and the you get the wrong UTC offset when calling the GetUtcOffset method with a DateTime of kind UTC. The local time and UTC offset are never wrong by more than the DST delta.

The bug is related to the change from one adjustment rule to another and seems to only affect floating adjustment rules where DST end is before DST start. E.g. DST ends 1th of March and starts 1th of October.

The conversion only fails in the first hours after a new adjustment rule has come into effect. The number of hours the conversion fails seems to be equivalent to the UTC offset for the time zone in question.

I've identified problems with the following time zones:
Ekaterinburg Standard Time at local time 2011-01-01 00:00:00
Fiji Standard Time at local time 2010-01-01 00:00:00
Jordan Standard Time at local time 2012-01-01 00:00:00
Kaliningrad Standard Time at local time 2011-01-01 00:00:00
Magadan Standard Time at local time 2011-01-01 00:00:00
Mauritius Standard Time at local time 2009-01-01 01:00:00
N. Central Asia Standard Time at local time 2011-01-01 00:00:00
Namibia Standard Time at local time 2011-01-01 00:00:00
North Asia East Standard Time at local time 2011-01-01 00:00:00
North Asia Standard Time at local time 2011-01-01 00:00:00
Russian Standard Time at local time 2011-01-01 00:00:00
Samoa Standard Time at local time 2011-01-01 00:00:00
Vladivostok Standard Time at local time 2011-01-01 00:00:00
W. Australia Standard Time at local time 2007-01-01 00:00:00
Yakutsk Standard Time at local time 2011-01-01 00:00:00
Sign in to post a comment.
Posted by Tarek [MSFT] on 2/25/2013 at 7:02 PM
Thank you for reporting this bug. Although this is corner case but it is valid bug and we’ll look at it in the future releases. This case will happen with the time zones which started to apply daylight saving in specific year.
For example, W Australia Perth time zone started applying daylight saving on December 2006 so the time zone rule for 2006 will have the daylight saving start at 12/1 and ends at 1/1 (to the end of the year). This causes the corner case seeing here. I said this is a corner case because if changing the ticks on the UTC by one tick that will make everything work as expected. here is some suggested workaround:

        static bool DetectSpecialCase(TimeZoneInfo zone, int year)
        {
            TimeZoneInfo.AdjustmentRule[] rules = zone.GetAdjustmentRules();
            for (var i = 0; i < rules.Length; i++)
            {
                if (rules[i].DateStart.Year >= year && rules[i].DateEnd.Year <= year)
                {
                    if (rules[i].DaylightTransitionEnd.Day == 1 && rules[i].DaylightTransitionEnd.Month==1)
                        return true;
                    return false;
                }
            }
            return false;
        }


     var timeToTest = new DateTime(2007, 1, 1, 0, 0, 0);
     var timeZone = TimeZoneInfo.FindSystemTimeZoneById("W. Australia Standard Time");
     var timeInUtc = TimeZoneInfo.ConvertTimeToUtc(timeToTest, timeZone);
     bool specialCase = DetectSpecialCase(timeZone, timeInUtc.Year);
     if (specialCase)
         timeInUtc = timeInUtc.AddTicks(-1); // adjust the ticks to make it repro the expected result
     var convertedTime = TimeZoneInfo.ConvertTimeFromUtc(timeInUtc, timeZone);
     if (specialCase)
         convertedTime = convertedTime.AddTicks(1); // adjust the ticks to make it repro the expected result
Posted by Helen [MSFT] on 2/10/2013 at 9:23 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 2/8/2013 at 5:50 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)