axelson

axelson

Scenic Core Team

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/1 and check if it returns {:error, :time_zone_not_found}
  • B: Directly use a time zone library like tz or tzdata

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

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” :slight_smile:

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 which exports a TzExtra.time_zone_id_exists?/1 helper.

LostKobrakai

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 :slight_smile:

Where Next?

Popular in Questions Top

pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement