At my workplace we have a a module of code that does essentially a mapping of two character codes two a specific city in the IANA time zone database.
For example, we have this kind of code:
def tzdb_timezone("CA-ON"), do: "America/Toronto"
def tzdb_timezone("US-NC"), do: "America/New_York"
Does an existing elixir/erlang library do the same?
I’ve done some research on it.
- I’ve looked at ex_cldr and ex_cldr_territories to no avail
- I have looked at the IANA tz data itself for a mapping of it to no avail
- tz and tzdata to no avail
Any additional information or links to existing elixir libraries or Erlang libraries would be greatly appreciated! Thank you in advance.
Ontario from your example lies in 3 time zones. I don’t think you can do a mapping like that which would be generic enough.
3 Likes
mayel
3
If you can get geocoordinates instead of just a state you could use this:
https://hexdocs.pm/tz_world/TzWorld.html#timezone_at/2
4 Likes
kip
4
If you can provide a UN LOCODE then you can resolve the applicable timezone, or timezones, by calling Cldr.Timezone.fetch/1.
locode
is quite useful in lots of applications so maybe that’s a good canonical approach for your requirements too?
Example
iex> Cldr.Timezone.fetch("ausyd")
{:ok,
["Australia/Sydney", "Australia/ACT", "Australia/Canberra", "Australia/NSW"]}
iex> Cldr.Timezone.fetch("uslax")
{:ok, ["America/Los_Angeles", "US/Pacific", "US/Pacific-New", "PST8PDT"]}
iex> Cldr.Timezone.fetch("nope")
:error
kip
5
And if locode
isn’t workable for you, you could map your coding to locode
s. The data to help with that can be retrieved with:
iex> Cldr.Timezone.timezones() |> Enum.filter(fn {k, _v} -> String.starts_with?(k, "us") end)
[
{"usmnm", ["America/Menominee"]},
{"ustel", ["America/Indiana/Tell_City"]},
{"usind",
["America/Indianapolis", "America/Fort_Wayne",
"America/Indiana/Indianapolis", "US/East-Indiana"]},
{"usden",
["America/Denver", "America/Shiprock", "Navajo", "US/Mountain", "MST7MDT"]},
{"ushnl", ["Pacific/Honolulu", "US/Hawaii", "Pacific/Johnston", "HST"]},
{"uswsq", ["America/Indiana/Petersburg"]},
...
]
1 Like