Long form name of timezone in NodaTime

后端 未结 3 831
灰色年华
灰色年华 2021-01-18 23:56

In NodaTime, how do you find the long form name of a timezone given the tz timezone id?

For example, if I supply \"America/Los_Angeles\", I should get \"Pacific Stan

3条回答
  •  清歌不尽
    2021-01-19 00:28

    The information you need to produce the "long form" of a time zone name isn't in Noda Time, but it can be found in the CLDR.

    I've recently put together a library called simply "Time Zone Names", that embeds the CLDR time zone names. You can use these with the IANA (TZDB) identifiers that are used by Noda Time time zones.

    • Install from NuGet
    • Source on GitHub
    • See the unit tests for example usage and output.

    Simply pass the time zone and language, and it will provide the appropriate generic name, standard name, and daylight name. You can use Noda Time to decide which form is appropriate to display.

    var names = TimeZoneNames.GetNamesForTimeZone("America/Los_Angeles", "en-US");
    
    Assert.Equal("Pacific Time", names.Generic);
    Assert.Equal("Pacific Standard Time", names.Standard);
    Assert.Equal("Pacific Daylight Time", names.Daylight);
    

    For the language, you can pass either a two digit code like "en", or you can pass a fully regionalized version such as "en-US". This aligns with CultureInfo names, so you can pass CultureInfo.CurrentUICulture.Name if you like.

提交回复
热议问题