.NET TimeZoneInfo wrong about Daylight savings

亡梦爱人 提交于 2019-12-01 19:32:30

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.

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.

TimeZoneInfo.GetUtcOffset(DateTime)

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!