TimeZoneInfo.Local vs TimeZoneInfo.FindSystemTimeZoneById

后端 未结 2 907
予麋鹿
予麋鹿 2021-02-13 15:34

I\'ve been working with the DateTime and TimeZoneInfo classes and I ran into an interesting result with the following code:

var dstStar         


        
2条回答
  •  盖世英雄少女心
    2021-02-13 15:56

    I did a little bit of reflecting and I believe the inconsistency stems from how System.TimeZoneInfo+CachedData.GetCorrespondingKind(TimeZoneInfo timeZone) returns DateTimeKind.Local only in the case where timeZone == this.m_localTimeZone (ie, when the argument was the same instance as the TimeZoneInfo.Local property is based on).

    In the case where you pass this other TimeZoneInfo instance you got from TimeZoneInfo.FindSystemTimeZoneById I expect that it returns DateTimeKind.Unspecified.

    This will (probably among other things) affect System.TimeZoneInfo.IsDaylightSavingTime(DateTime dateTime) where, in the case where dateTime.Kind is local, it does a conversion between essentially TimeZoneInfo.Local and your TimeZoneInfo instance and bases the conversion on what GetCorrespondingKind says for the source and target timezones (the conversion returns the original datetime in the case where source and target are both local).

提交回复
热议问题