Vincent Vincent - 2 months ago 8
Python Question

Get all available timezones

I'm currently working on an application that is required to support multiple timezones.

For that, I'm using the dateutil library. Now, I need a way to present the user a list of all available timezones the dateutil library supports.

Also, is it advisable to store the tz as strings in the database like "Europe/Berlin"?

Answer

dateutil will use the OS timezone info, but does carry it's own compressed timezone info file too.

You could list the available names from that file:

import os
import tarfile
import dateutil.zoneinfo

zi_path = os.path.abspath(os.path.dirname(dateutil.zoneinfo.__file__))
zonesfile = tarfile.TarFile.open(os.path.join(zi_path, 'zoneinfo--latest.tar.gz'))
zonenames = zonesfile.getnames()

You may need to sort and filter that list a little; there are both abbreviated (3 letter) timezone codes as well as "region/city" entries in this list.

Storing those names in a database is just fine.