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?
To access a list of all the timezones stored in the pytz module, use 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 access, for example, the European timezones in the timezone list with the following code:
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']
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:
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'})
Get actionable, code-level insights to resolve Python performance bottlenecks and errors.
Create a free Sentry account
Create a Python project and note your DSN
Grab the Sentry Python SDK
pip install --upgrade sentry-sdk
import sentry_sdk sentry_sdk.init( "https://<key>@sentry.io/<project>", # Set traces_sample_rate to 1.0 to capture 100% # of transactions for performance monitoring. # We recommend adjusting this value in production. traces_sample_rate=1.0, )
Loved by over 4 million developers and more than 90,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.