Querying for Timezones in Timex or some other library?

I’ve often found it helpful to be able to reference timezones for humans using an app. E.g. we have a database record for a person or a store location and we want to know what timezone they are in.

When I use Timex, I see that it updates “tzdata” constantly in little files named something like 2018e.ets. Is there any way I can search or reference that data? For BI/UI/transparency purposes, I’d prefer to have it available in a relational database, but I’ve never been able to figure out how to do that. Even MySQL had internal timezone tables somewhere. Does anyone know how to get at that data? E.g. say I want to do an auto-complete on a form field or reference the timezone as a foreign key constraint. Possible?

The data comes from here: https://www.iana.org/time-zones


It looks like Timex depends on another project called tzdata. That project includes functions that let you query time zone data.

iex(2)> Tzdata.zone_list()
["Africa/Abidjan", "Africa/Accra", "Africa/Addis_Ababa", "Africa/Algiers",
 "Africa/Asmara", "Africa/Asmera", "Africa/Bamako", "Africa/Bangui",
 "Africa/Banjul", "Africa/Bissau", "Africa/Blantyre", "Africa/Brazzaville",
 "Africa/Bujumbura", "Africa/Cairo", "Africa/Casablanca", "Africa/Ceuta",
 "Africa/Conakry", "Africa/Dakar", "Africa/Dar_es_Salaam", "Africa/Djibouti",
 "Africa/Douala", "Africa/El_Aaiun", "Africa/Freetown", "Africa/Gaborone",
 "Africa/Harare", "Africa/Johannesburg", "Africa/Juba", "Africa/Kampala",
 "Africa/Khartoum", "Africa/Kigali", "Africa/Kinshasa", "Africa/Lagos",
 "Africa/Libreville", "Africa/Lome", "Africa/Luanda", "Africa/Lubumbashi",
 "Africa/Lusaka", "Africa/Malabo", "Africa/Maputo", "Africa/Maseru",
 "Africa/Mbabane", "Africa/Mogadishu", "Africa/Monrovia", "Africa/Nairobi",
 "Africa/Ndjamena", "Africa/Niamey", "Africa/Nouakchott", "Africa/Ouagadougou",
 "Africa/Porto-Novo", "Africa/Sao_Tome", ...]

As far as I understand the concept, that’s only a cache directly usuable from code and directly loadable into an ETS table.

If you deleithe files, then a new cache should get populated as you use your application.

There is no actual need to store them anywhere, it just makes your app a little bit faster when answering timezone queries.

Actually, per my OP, I do have a need to store that somewhere: I want to be able to reference it as a foreign key in other tables.

You can get a list of timezones as it was shown already, you can use those in your DB as you like.

But thats not actually timezone data.

TZdata is a historic collection about when timezones changed. When leap seconds have been added, when timezones got deleted or created. When regions denied to switch to DST. etc, etc. Nothing what you could use as a key.

But you can query this information directly, just look at the API of the TZdata package you use.

1 Like