NodaTime time zone long name

In NodaTime, how do you find the name of the long form of the time zone given the timezone identifier tz?

For example, if I put America / Los_Angeles, I have to return Pacific Standard Time.

+6
source share
3 answers

The information needed to create a “long form” of the time zone name is not in Noda Time, but it can be found in CLDR .

I recently put together a library called simply “time zone names” that includes CLDR time zone names. You can use them with IANA Identifiers (TZDB), which are used by Noda Time time zones.

Just pass the time zone and language and it will provide the corresponding common name, standard name and daylight name. You can use Noda Time to decide which form is suitable for 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 a language, you can either pass a two-digit code, for example, "en" , or you can pass a fully regional version, for example, "en-US" . This matches the CultureInfo names, so you can pass CultureInfo.CurrentUICulture.Name if you want.

+6
source

TZDB itself does not contain a description for time zones: the time zone with the identifier America/Los_Angeles simply contains transitions with names such as "PDT" and "PST". Therefore, from this point of view, there is simply no data.

Thus, you can get the Windows time zone identifiers that are displayed in this TZDB zone (originally from the CLDR data windowsZones.xml ), and Windows usually uses Pacific Standard Time names for its zone identifiers.

eg.

 var source = TzdbDateTimeZoneSource.Default; var windowsIds = (from item in source.WindowsMapping.PrimaryMapping where item.Value == "America/Los_Angeles" select item.Key).ToList(); 

However, there are some caveats to this approach:

  • As shown above, there can be any number of Windows zone identifiers that are displayed in a given TZDB zone. In the current data it is always zero or one ( Europe/Vienna is an example of a TZDB zone that the Windows zone identifier does not use), but in theory there is no reason to believe that you could not find two or more Windows zone identification numbers for the same TZDB zone .
  • Some Windows zone identifiers are not particularly good: for example, Europe/London maps to a Windows zone called GMT Standard Time, which is not a great string to display to the user.

However, for what you are doing, this may be acceptable.

+2
source

Short answer: https://github.com/barrycarter/bcapps/blob/master/ASTRO/tz2name.txt

Long answer: as others have noted, you can use the common/supplemental/metaZones.xml to display time zones in regions. For instance:

  <timezone type="America/Barbados"> <usesMetazone mzone="Atlantic"/> </timezone> 

displays the time zone "America / Barbados" in the "Atlantic" region.

Then you can use common/main/en.xml to convert the region to the time zone name. For instance:

  <metazone type="Atlantic"> <long> <generic>Atlantic Time</generic> <standard>Atlantic Standard Time</standard> <daylight>Atlantic Daylight Time</daylight> </long> <short> <generic>AT</generic> <standard>AST</standard> <daylight>ADT</daylight> </short> </metazone> 

tells us the names of time zones (long and short forms) used in the "Atlantic" region.

This is pretty much a repetition of the other answers, but with direct links to the files in question.

0
source

Source: https://habr.com/ru/post/973237/


All Articles