NodaTime: Time zone related issue using NodaTime library c#

蹲街弑〆低调 提交于 2019-12-12 10:31:37

问题


here i am giving my code and what happen.

when i am passing timezone id to .net time zone that works the code as below

    var zoneId = "India Standard Time";
    var zone = TimeZoneInfo.FindSystemTimeZoneById(zoneId);
    var now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, zone);
    string xx1 = now.ToLongTimeString();

when i am passing the same time zone id India Standard Time to noda time library then i am getting error "Time zone India Standard Time is unknown to source TZDB: 2014e (mapping: 9723)"

my code as follows for noda time

    var zoneId = "India Standard Time";
    DateTimeZone _zone = DateTimeZoneProviders.Tzdb[zoneId];
    ZonedDateTime _now = SystemClock.Instance.Now.InZone(_zone);
   string xx= now.ToLongTimeString();

just tell me how to pass timezone to noda library for India Standard Time or GMT Standard Time

thanks


回答1:


If you want to pass a BCL time zone to Noda Time, you just need to use the BCL provider:

DateTimeZone _zone = DateTimeZoneProviders.Bcl[zoneId];

This will find the relevant TimeZoneInfo, extract its adjustment rules, and convert that into a Noda Time representation. You can then use it just like any other DateTimeZone.

Note that these time zone IDs are Windows-specific. If you can uses the IANA (TZDB) time zone IDs instead, that would generally make your data more portable to other systems.




回答2:


As the error message says, the string you supplied is not in the tzdb (Olson database). There is a list of zones on Wikipedia: Indian Standard Time is "Asia/Kolkata". Try that as your zone string.

"Etc/GMT" is the string for GMT which wiki says is a shortcut to the timezone string "UTC".



来源:https://stackoverflow.com/questions/26626798/nodatime-time-zone-related-issue-using-nodatime-library-c-sharp

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