Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET TimeZoneInfo wrong about Daylight savings

Tags:

timezone

.net

dst

Can anyone help make sense of this. Microsoft’s TimeZoneInfo class in 3.5 is telling me the following GMT date is not in Daylight savings for the Eastern timezone, but it is.

// Get Eastern Timezone
TimeZoneInfo tzEasternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

// Convert to EST 
DateTime easternTime = TimeZoneInfo.ConvertTime(DateTime.Parse("2009-11-01T05:00:00Z"), tzEasternZone);

// Daylight saving IS in effect on this date, but Microsoft doesn't think so
Boolean isDaylight = easternTime.IsDaylightSavingTime();

Here are 2 websites saying it is in daylight savings:
http://www.timeanddate.com/worldclock/converted.html?month=11&day=1&year=2009&hour=5&min=0&sec=0&p1=0&p2=198
http://www.timezoneconverter.com/cgi-bin/tzc.tzc

like image 701
Matt Avatar asked Feb 14 '11 03:02

Matt


People also ask

How does daylight savings affect time zones?

The simplest way to remember "time zone math" for the states that recognize Daylight saving time is three-two-one: three hours difference from Eastern to Pacific, two hours difference from Eastern to Mountain, and one hour difference from Eastern to Central.

Does salesforce adjust for Daylight Savings?

If the option selected includes Daylight Time, then yes, Salesforce will automatically update the time to reflect Daylight Savings Time. If Standard Time is selected, then the time will always remain according to Standard Time and will not adjust with Daylight Savings.

How do you test for DST?

Test transition of DST, i.e. when you are currently in summer time, select a time value from winter. Test boundary cases, such as a timezone that is UTC+12, with DST, making the local time UTC+13 in summer and even places that are UTC+13 in winter.


3 Answers

See: TimeZoneInfo.IsDaylightSaving

Because the TimeZoneInfo.IsDaylightSavingTime(DateTime) method can return false for a date and time that is ambiguous (that is, a date and time that can represent either a standard time or a daylight saving time in a particular time zone), the TimeZoneInfo.IsAmbiguousTime(DateTime) method can be paired with the IsDaylightSavingTime(DateTime) method to determine whether a time may be a daylight saving time. Because an ambiguous time is one that can be both a daylight saving time and a standard time...

Also you might look at this...

TimeZoneInfo.GetAmbiguousTimeOffsets Method

Returns information about the possible dates and times that an ambiguous date and time can be mapped to.

like image 107
John Sobolewski Avatar answered Oct 06 '22 04:10

John Sobolewski


This will work:

TimeZoneInfo tzEasternZone = TimeZoneInfo.FindSystemTimeZoneById(
                                          "Eastern Standard Time");

DateTime utc = DateTime.Parse("2009-11-01T05:00:00Z",
                              CultureInfo.InvariantCulture,
                              DateTimeStyles.RoundtripKind);

bool isDaylight = tzEasternZone.IsDaylightSavingTime(utc);

The original code had two issues:

  • Even though a UTC value was being provided, it was getting converted to local kind in the Parse statement. So ambiguity could be introduced there.

  • The IsDaylightTime method on the DateTime class will assume the local time zone if the kind is local or unspecified. After calling ConvertTime, the result has unspecified kind, so it was checking against the rules of the local time zone, not the eastern time zone.

like image 43
Matt Johnson-Pint Avatar answered Oct 06 '22 03:10

Matt Johnson-Pint


TimeZoneInfo.GetUtcOffset(DateTime)

Correctly returns the offset with daylight savings factored in if the specified date is inside the period

like image 44
Andrew Harry Avatar answered Oct 06 '22 05:10

Andrew Harry