Home Dashboard Directory Help
Search

TimeZone.GetUtcOffset and DateTime.ToUniversalTime() not consistent for brief period for Atlantic Time Zone by remids


Status: 

Closed
 as Won't Fix Help for as Won't Fix


1
0
Sign in
to vote
Type: Bug
ID: 699491
Opened: 11/6/2011 7:15:54 PM
Access Restriction: Public
0
Workaround(s)
view
0
User(s) can reproduce this bug

Description

I believe I have found a timezone-related bug in the .Net framework. My assumption in the following NUnit test is that the UTC offset for a given date should be the same whether fetched from the TimeZone or by converting a local date to UTC, and fetching the difference.

I am in the Atlantic Time Zone.

        [Test, Sequential]
        public void AnotherTest(
            [Values(2006, 2006, 2006, 2006, 2006, 2006, 2006)] Int32 year,
            [Values(10, 10, 10, 10, 11, 11, 11)] Int32 month,
            [Values(26, 27, 30, 31, 1, 2, 3)] Int32 day)
        {
            var dt = new DateTime(year, month, day, 0, 0, 0, DateTimeKind.Local);

            var utcOffset1 = TimeZone.CurrentTimeZone.GetUtcOffset(dt);
            var utcOffset2 = dt.Subtract(dt.ToUniversalTime());

            Assert.AreEqual(utcOffset1, utcOffset2);
        }

These pass on Oct 26 and Nov 3, and fail for the dates between. I also ran tests for various earlier years, with same results. For dates from 2007 through 2011, these all pass. For all failures I've found, utcOffset1 was -3 hours while utcOffset2 was -4 hours. (According to <http://www.timeanddate.com/library/abbreviations/timezones/>, AST is supposed to be UTC-4 and ADT should be UTC-3.)

NOTE: When daylight savings was changed to 34 weeks in 2007, it also starts earlier. I haven't looked into it as much, but dates I've tried between March 10 and April 4 appears to indicate a problem at that end as well.
Details
Sign in to post a comment.
Posted by Microsoft on 5/10/2012 at 9:21 PM
Hi!

Thanks for bringing up this interesting issue. We are always grateful when customers point towards potential concerns - this helps us ensuring the quality of the .NET Framework and driving the product into the right direction.

I see from the comment below that you have already identified the relevant piece of documentation.
TimeZone does not work well with historical DST information. Moreover, DateTime and TimeZone do not work well with issues related to the missing and the ambiguous hour during the DST transitions.

All these issues should be resolved in the DateTimeOffset and TimeZoneInfo types. I expect, your tests should work correctly with those types.
I will close the bug for now, but if you observe some incorrect behaviour with the abovementioned types as well, please feel free to reactivate or to file a new submission.

I hope this helps.

Greg
(Software Engineer on the .NET Base Class Libraries team)
Posted by Microsoft on 5/10/2012 at 5:19 AM
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 remids on 11/10/2011 at 4:37 AM
Further investigation found that 5 of 7 tests fail only in .Net 4.

However, a colleague of mine found this nugget which appears to explain it: http://msdn.microsoft.com/en-us/library/system.timezone.getutcoffset.aspx

"The GetUtcOffset method recognizes only the current daylight saving time adjustment rule for the local time zone. As a result, it is guaranteed to accurately return the UTC offset of a local time only during the period in which the latest adjustment rule is in effect. It may return inaccurate results if Time is a historic date and time value that was subject to a previous adjustment rule."
Posted by remids on 11/9/2011 at 4:58 AM
The tests are outlined exactly as noted in the case above, using NUnit 2.5. Did you remember to change your machine's time zone to the Atlantic Time Zone before attempting the test?
Posted by MS-Moderator08 on 11/8/2011 at 1:40 AM
Thank you for reporting this issue. But we were not able to reproduce it with the steps you provided. Could you please attach a sample project about this issue?
Posted by MS-Moderator01 on 11/6/2011 at 7:42 PM
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)
Sign in to post a workaround.