AlignedDev
AlignedDev

Reputation: 8233

C# TimeZoneInfo.ConvertTimeToUtc doesn't match other time conversion tools

I am running this code snippet in LINQPad.

DateTime dt = new DateTime(2011, 2, 1,15,0,0);
dt.Dump();
TimeZoneInfo timeZoneId = TimeZoneInfo.FindSystemTimeZoneById("Samoa Standard Time");
//timeZoneId.GetAdjustmentRules().Dump();
var x = TimeZoneInfo.ConvertTimeToUtc(DateTime.SpecifyKind(dt, DateTimeKind.Unspecified), timeZoneId);
x.Dump();

The result is: 2/1/2011 3:00:00 PM 2/2/2011 1:00:00 AM

When I go to a GMT Time Convertor to double check, the returned value is: 2/1/2011 2:00:00 AM. Why does the .Net function return a different value than the other site. Who is correct? We just switched to Daylight Savings Time (I didn't check it before switching), does that matter?

Our QA found a list of mismatches:

ID  GMT Offset  Name                DateTime Tested     Conversion Result   Correct GMT
                                        (Value in DB)
16  -11:00      Samoa               2/1/2011 3:00 PM    2/2/2011 1:00 AM    2/2/2011 2:00 AM
19  +12:00      Auckland, Wellington 2/1/2011 3:00 PM   2/1/2011 2:00 AM    2/1/2011 3:00 AM
20  +12:00      Fiji                2/1/2011 3:00 PM    2/1/2011 2:00 AM    2/1/2011 3:00 AM
23  +10:00      Canberra,           2/1/2011 3:00 PM    2/1/2011 4:00 AM    2/1/2011 5:00 AM
                Melbourne, Sydney
25  +10:00      Hobart              2/1/2011 3:00 PM    2/1/2011 4:00 AM    2/1/2011 5:00 AM
27  +09:30      Adelaide            2/1/2011 3:00 PM    2/1/2011 4:30 AM    2/1/2011 5:30 AM
68  +01:00      Windhoek            2/1/2011 3:00 PM    2/1/2011 1:00 PM    2/1/2011 2:00 PM
80  -03:00      Brasilia            2/1/2011 3:00 PM    2/1/2011 5:00 PM    2/1/2011 6:00 PM
84  -03:00      Montevideo          2/1/2011 3:00 PM    2/1/2011 5:00 PM    2/1/2011 6:00 PM
88  -04:00      Cuiaba              2/1/2011 3:00 PM    2/1/2011 6:00 PM    2/1/2011 7:00 PM
89  -04:00      Santiago            2/1/2011 3:00 PM    2/1/2011 6:00 PM    2/1/2011 7:00 PM

Upvotes: 2

Views: 1916

Answers (1)

AlignedDev
AlignedDev

Reputation: 8233

It turns out that this is just a difference in DaylightSavingsTime (or their version for these countries. If the dt in the example is changed to DateTime dt = new DateTime(2011, 4, 3,15,0,0);, the returned time is 2:00 am, the GetAdjusmentRules() method shows a transition time from 9/1/11 to 4/1/11, so after 4/1 their version of DaylightSavingsTime kicks in.

Upvotes: 1

Related Questions