Convert NodaTime DateTimeZone into TimeZoneInfo

后端 未结 4 1989
天涯浪人
天涯浪人 2021-02-14 04:40

I\'m using NodaTime because of its nice support for zoneinfo data, however I have a case where I need to convert the DateTimeZone into TimeZoneInfo for

4条回答
  •  梦谈多话
    2021-02-14 05:13

    You can use TimeZoneConverter library by Matt Johnson.

    ZoneId used by NodeTime TzdbZoneLocation is IANA time zone, so you can get TimeZoneInfo like this:

    string windowsTimeZoneName = TZConvert.IanaToWindows(tzdbZoneLocation.ZoneId);
    var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(windowsTimeZoneName);
    

    Don't forget to wrap it with try-catch with some kind of fallback just in case.

    Also look at original Matt Johnson solution for converting between IANA time zone and Windows time zone.

提交回复
热议问题