Get all available timezones
Asked Answered
T

2

7

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"?

Tatianatatianas answered 16/3, 2013 at 19:46 Comment(3)
pytz.all_timezones gives a list of the timezone names that can be used with pytz. Sometimes timezone conversions with dateutil and pytz can produce different results.Sent
You may want to consider a map-based timezone picker instead of a list. Here's one for JavaScript. If you're not writing a web app, I'm sure somewhere there is a native python control that is similar, but I don't know of one off hand.Rutan
related: Python: datetime tzinfo time zone names documentationParasitic
A
12

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 the utility code that loads this data:

from dateutil.zoneinfo import get_zonefile_instance
zonenames = list(get_zonefile_instance().zones)

You'd 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.

Note that this loads all data into memory; if that's not desirable you'd need to load the tar file yourself:

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, dateutil.zoneinfo.ZONEFILENAME))
zonenames = [zn.name for zn in zonesfile.getmembers()
             if not zn.isdir() and zn.name != dateutil.zoneinfo.METADATA_FN]
Avram answered 16/3, 2013 at 20:10 Comment(2)
For dateutil 2.6.0, the TAR file has a different file name: dateutil-zoneinfo.tar.gz.Gertrudegertrudis
@KurtPeek: thanks; the code now includes easier-to-access utility code to load this data directly, avoiding the need to hardcode the filename.Avram
M
2

For dateutil:

from dateutil.zoneinfo import getzoneinfofile_stream, ZoneInfoFile
ZoneInfoFile(getzoneinfofile_stream()).zones.keys()
>>> ['Asia/Ujung_Pandang',
     'America/Porto_Velho',
     'America/La_Paz',
     'America/Caracas',
     'Europe/Malta',
     'Etc/GMT-13',
     'Atlantic/Bermuda',
     ...]

pytz is a little easier

import pytz
pytz.all_timezones
>>> ['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',
     ...]

Note that dateutil prefers to use the operating system to determine timezone info. pytz uses its own zone files. While there are probably advantages to each, pytz provides more consistency if you don't want to manage time zones at the system level.

Multitudinous answered 21/9, 2016 at 23:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.