Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long form name of timezone in NodaTime

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 Standard Time" back.

like image 342
govin Avatar asked Aug 01 '14 17:08

govin


2 Answers

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.

like image 198
Matt Johnson-Pint Avatar answered Nov 14 '22 22:11

Matt Johnson-Pint


TZDB itself doesn't hold descriptions for timezones: the timezone with ID America/Los_Angeles simply holds transitions with names such as "PDT" and "PST". So from that point of view, the data simply isn't there.

That said, you can get the Windows timezone IDs that map to a given TZDB zone (originally from the CLDR windowsZones.xml data), and Windows generally does use names like "Pacific Standard Time" for its zone IDs.

e.g.

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 with this approach:

  • As shown above, there may be any number of Windows zone IDs that map to a given TZDB zone. In the current data, this is always zero or one (Europe/Vienna being an example of a TZDB zone that no Windows zone ID uses), but there's no reason in theory that you couldn't find two or more Windows zone IDs mapping to the same TZDB zone.
  • Some of the Windows zone IDs aren't particularly great: for example, Europe/London is mapped from a Windows zone called "GMT Standard Time", which isn't a great string to show to a user.

However, for what you're doing, this might be acceptable.

like image 45
Malcolm Rowe Avatar answered Nov 14 '22 23:11

Malcolm Rowe