.NET TimeZoneInfo wrong about Daylight savings

前端 未结 3 1096
夕颜
夕颜 2021-01-19 06:53

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 i

3条回答
  •  旧时难觅i
    2021-01-19 07:12

    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.

提交回复
热议问题