axelson
How can you check if a timezone is valid?
In Elixir core is there a function to check if a given timezone is valid?
I’m mainly looking at DateTime and Calendar but as far as I can tell they both don’t have a way to directly verify that a given String is a valid time zone.
I think there’s two options here:
- A: Use a function like
DateTime.now/1and check if it returns{:error, :time_zone_not_found} - B: Directly use a time zone library like
tzortzdata
I suppose A is the way to go and is not that odd. But it does feel a little weird that there isn’t a way to directly check the validity of a timezone. And it looks like that function can only return two errors {:error, :time_zone_not_found | :utc_only_time_zone_database} so as long as I ensure that I setup my time zone database correctly I can use DateTime.now/1 to check the validity of a time zone.
Most Liked
ruslandoga
There is probably going to be a “wasted” DateTime → Gregorian days conversion with DateTime.now approach. Not that it’s expensive, just feels “wrong” ![]()
I don’t know about Tzdata, but for Tz there is GitHub - mathieuprog/tz_extra: A few utilities to work with time zones in Elixir · GitHub which exports a TzExtra.time_zone_id_exists?/1 helper.
LostKobrakai
You eventually might need to provide a datetime for the check as well – given I expect the list to not be static over time. And at that point DateTime.from_naive kinda does what you need ![]()
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex










