.NET TimeZoneInfo error regarding daylight saving

Can someone help figure this out. The Microsoft TimeZoneInfo class in 3.5 tells me that the next GMT date does not apply to summer savings for the eastern time zone, 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's in daylight:
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

+3
source share
3 answers

See: TimeZoneInfo.IsDaylightSaving

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

You can also look at this ...

Method TimeZoneInfo.GetAmbiguousTimeOffsets

Returns information about possible dates and times for which a two-digit date and time can be matched.

+5
source

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); 

There were two problems in the source code:

  • Although a UTC value was provided, it was converted to a local view in the Parse statement. Therefore, ambiguity can be introduced there.

  • The IsDaylightTime method in the DateTime class will take a local time zone if the view is local or unspecified. After calling ConvertTime result is unspecified, so it checked the local time zone rules, not the eastern time zone.

+4
source

TimeZoneInfo.GetUtcOffset(DateTime)

Correctly returns the offset, taking into account the daily savings, if the specified date is inside the period

+1
source

Source: https://habr.com/ru/post/1384326/


All Articles