Is There a List of pytz Timezones?

James W.
jump to solution

The Problem

Python applications often need to work with timezones for date-time conversion and timezone calculations, staying accurate to the timezone of a particular location.

But if you want to make use of a specific timezone, or access a list of popular timezones, how do you list available timezones in Python?

The Solution

Since Python 3.9, the zoneinfo module is part of the standard library, so no extra dependency is needed. Use zoneinfo.available_timezones() to get a set of all available IANA timezone names:

from zoneinfo import available_timezones

print(sorted(available_timezones()))

Output (shortened):

['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', ... 'US/Samoa', 'UTC', 'W-SU', 'WET', 'Zulu']

Because available_timezones() returns a set, you can filter it the same way. For example, to list European timezones:

from zoneinfo import available_timezones

europe_timezones = sorted(tz for tz in available_timezones() if tz.startswith("Europe/"))
print(europe_timezones)

Output (shortened):

['Europe/Amsterdam', 'Europe/Andorra', 'Europe/Astrakhan', 'Europe/Athens', ... 'Europe/Warsaw', 'Europe/Zagreb', 'Europe/Zaporozhye', 'Europe/Zurich']

Using pytz (for projects that already use pytz)

If your project already depends on pytz, you can list timezones with the all_timezones attribute:

import pytz

print(pytz.all_timezones)

This attribute returns a list of all the time zones supported by the pytz module.

Output (shortened):

['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', ... 'US/Samoa', 'UTC', 'Universal', 'W-SU', 'WET', 'Zulu']

You can also filter for specific regions, for example European timezones:

import pytz

europe_timezones = [timezone for timezone in pytz.all_timezones if "Europe" in timezone]
print(europe_timezones)

Output (shortened):

['Europe/Amsterdam', 'Europe/Andorra', 'Europe/Astrakhan', 'Europe/Athens', ... 'Europe/Warsaw', 'Europe/Zagreb', 'Europe/Zaporozhye', 'Europe/Zurich']

pytz also provides the common_timezones_set attribute that returns commonly used timezones as a set:

import pytz

common_timezones = pytz.common_timezones_set
print(common_timezones)

Output (shortened):

LazySet({'America/Indiana/Vevay', 'US/Arizona', 'Asia/Yerevan', ... 'Asia/Chita', 'Pacific/Saipan', 'Africa/Nouakchott'})
Add new keys to a dictionary in Python
David Y.
Convert an integer to a string in Python
David Y.
Convert a list to a string in Python
David Y.

Considered "not bad" by 4 million developers and more than 150,000 organizations worldwide, Sentry provides code-level observability to many of the world's best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

Sentry