In this tutorial, we will learn how to get the list of timezones available in the world in Python.
The pytz library has implemented a timezone class for handling arbitrary fixed offsets from UTC and timezones.
pytz has a huge list of timezones available in the world. This is helpful if you don’t know the exact name of the timezone to create a date and time in that timezone.
It provides a list with more than 500 names representing common timezones across the world.
This module has methods to create datetime objects with localized timezones. In addition to the base class methods, it has some of the following methods and attributes, useful in finding the supported timezone strings.
Below are the methods and attributes to get all timezone names: –
all_timezones
: Returns the list of all the timezones supported by the timezone moduleall_timezones_set
: Returns a set with all the timezones supported by the timezone modulecommon_timezones
andcommon_timezones_set
: Provides a list and set of the commonly used timezones.country_names
: Returns a dictionary with the country’s ISO Alpha 2 code as key and full country name as its value.country_timezones
: Returns a dictionary with Country code as Key and supported timezones as its value.
Read our complete guide on timezone in Python to work with the timezones.
Get List of All Timezones Name
First, Install the pytz module if not installed using the pip install pytz
command. Use the pytz.all_timezones
attribute to get the list of available timezone in the world.
import pytz
print('Timezones')
for timeZone in pytz.all_timezones:
print(timeZone)
Output:
Timezones Africa/Abidjan Africa/Accra Africa/Addis_Ababa ... US/Mountain US/Pacific US/Samoa UTC Universal Count: 592
There is another attribute that returns a set of timezones instead of a list.
import pytz
print('Timezones')
for timeZone in pytz.all_timezones_set:
print(timeZone)
Note: This list contains a lot of alias names, such as US/Central for the timezone that is properly called America/Mexico_City
Read our complete guide on timezone in Python
Get Common Timezones
The above list is vast. If you want to get the most commonly used timezone in the world, use the pytz.common_timezones
attribute.
import pytz
print('Most commonly used timezones')
for timeZone in pytz.common_timezones:
print(timeZone)
print(len(pytz.common_timezones))
# Output 440
Get Timezone of a Any Country
Use the pytz.country_timezones['country_name']
attribute to get the timezone of any country.
Let’s see how to get all the timezone of the united states.
import pytz
print('US TimeZones')
for timeZone in pytz.country_timezones['US']:
print(timeZone)
Output:
US TimeZones
America/New_York
America/Detroit
America/Kentucky/Louisville
America/Kentucky/Monticello
America/Indiana/Indianapolis
America/Indiana/Vincennes
America/Indiana/Winamac
America/Indiana/Marengo
America/Indiana/Petersburg
America/Indiana/Vevay
America/Chicago
America/Indiana/Tell_City
America/Indiana/Knox
America/Menominee
America/North_Dakota/Center
America/North_Dakota/New_Salem
America/North_Dakota/Beulah
America/Denver
America/Boise
America/Phoenix
America/Los_Angeles
America/Anchorage
America/Juneau
America/Sitka
America/Metlakatla
America/Yakutat
America/Nome
America/Adak
Pacific/Honolulu
Note: Use pytz.country_names
to get the all country names if you don’t know the exact name of the country.
import pytz
print('country Names with Code')
for code, name in pytz.country_names.items():
print(code, ':', name)
print('Country full name =', pytz.country_names['IN'])