Sentry Answers>Python>

Is There a List of pytz Timezones?

Is There a List of pytz Timezones?

James W.

The Problem

The pytz module allows for date-time conversion and timezone calculations so that your Python applications can keep track of dates and times, while 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, is there a way you can list timezones in the pytz module?

The Solution

To access a list of all the timezones stored in the pytz module, use the all_timezones attribute:

Click to Copy
import pytz print(pytz.all_timezones)

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

Output (shortened):

Click to Copy
['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', … 'US/Samoa', 'UTC', 'Universal', 'W-SU', 'WET', 'Zulu']

You can also access, for example, the European timezones in the timezone list with the following code:

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

Output (shortened):

Click to Copy
['Europe/Amsterdam', 'Europe/Andorra', 'Europe/Astrakhan', 'Europe/Athens', … 'Europe/Warsaw', 'Europe/Zagreb', 'Europe/Zaporozhye', 'Europe/Zurich']

The pytz module also contains the common_timezones attribute, or perhaps more useful, the common_timezones_set attribute that returns commonly used timezones already as a set:

Click to Copy
import pytz common_timezones = pytz.common_timezones_set print(common_timezones)

Output (shortened):

Click to Copy
LazySet({'America/Indiana/Vevay', 'US/Arizona', 'Asia/Yerevan', … 'Asia/Chita', 'Pacific/Saipan', 'Africa/Nouakchott'})
  • Sentry BlogPython Performance Testing: A Comprehensive Guide (opens in a new tab)
  • Syntax.fmListen to the Syntax Podcast (opens in a new tab)
  • Sentry BlogLogging in Python: A Developer’s Guide (opens in a new tab)
  • CodecovPython - Codecov (opens in a new tab)
  • Syntax.fm logo
    Listen to the Syntax Podcast (opens in a new tab)

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

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.