Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong daylight saving date when using TimeZoneInfo.ConvertTime? [closed]

On November 6th, 2011, at 2 AM (EST, e.g. in New York), it was 3 AM according to the daylight saving time schedule.

Does someone know why the TimeZoneInfo.ConvertTime function from the .NET framework returns the following:

var tzi = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var f = "yyyy/MM/dd HH:mm:ss";
TimeZoneInfo.ConvertTime(new DateTimeOffset(2012, 11, 3, 11, 0, 0, TimeSpan.Zero), tzi).ToString(f);
"2012/11/03 07:00:00"
TimeZoneInfo.ConvertTime(new DateTimeOffset(2012, 11, 4, 11, 0, 0, TimeSpan.Zero), tzi).ToString(f);
"2012/11/04 06:00:00"
TimeZoneInfo.ConvertTime(new DateTimeOffset(2012, 11, 6, 11, 0, 0, TimeSpan.Zero), tzi).ToString(f);
"2012/11/06 06:00:00"
TimeZoneInfo.ConvertTime(new DateTimeOffset(2012, 11, 7, 11, 0, 0, TimeSpan.Zero), tzi).ToString(f);
"2012/11/07 06:00:00"

It seems as if the time change occurred on the 4th of November, which makes no sense as it occurred on the 6th.

Edit: wow, this one is on me. I used 2012 instead of 2011 in my code.

like image 517
Erwin Mayer Avatar asked Sep 15 '26 13:09

Erwin Mayer


1 Answers

Look at your description:

On November 6th, 2011

Now look at your code:

new DateTimeOffset(2012, 11, 3, 11, 0, 0, TimeSpan.Zero)

I think we've found the problem.

Looking at the daylight saving time schedule for 2012 shows the change occurring on November 4th 2012, just as .NET says.

Of course, I'd still argue that the code would be clearer in Noda Time, but I'm biased :)

like image 61
Jon Skeet Avatar answered Sep 17 '26 03:09

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!