The following tutorial will show you have to get a Python Timezone List. We will be using the powerful pytz library for our code examples.
We will be using Python 3.8.10 in this tutorial.
SECTIONS
How Do We Get A List Of All Time Zones In pytz?
How Do We Get the Current Time Zone In pytz?
How Do We Find Current Time In Another Time Zone In pytz?
How Do I Set a Time Zone In pytz?
How Do We Get A List Of All Time Zones In pytz?
First we must install the pytz library. You can install it with the command pip install pytz at the command line if you don’t already have it. Find more information HERE.
After that, we simply run the following command:
import pytz
print(pytz.all_timezones)
print(len(pytz.all_timezones))
#outputs: ['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', 'Africa/Asmara', 'Africa/Asmera', 'Africa/Bamako', 'Africa/Bangui', 'Africa/Banjul'....]
#593
The first print statement will give us an output of type LazyList of all the time zones available in the library, but with their long name.
The second print statement will give us a count of the items in the list: 593
How Do We Get the Current Time Zone In pytz?
import pytz
from tzlocal import get_localzone
print(get_localzone())
#outputs: America/La_Paz
Using the datetime library and tzlocal library (you can find information on both HERE and HERE), this can be easily done. If you don’t have tzlocal you can install it with pip install tzlocal at the command line. The above code will give us the information we are looking for.
How Do We Find Current Time In Another Time Zone In pytz?
You can easily do this with the following code:
from datetime import datetime
from pytz import timezone
tz = timezone('America/Los_Angeles')
print(datetime.now(tz).strftime('%a %d %b %Y %I:%M:%S.%f %p %z'))
print(datetime.now(tz).strftime('%a %d %b %Y %I:%M:%S.%f %p %Z'))
#Outputs
#Mon 07 Jun 2021 06:37:21.808512 AM -0700
#Mon 07 Jun 2021 06:37:21.808512 AM PDT
tz = timezone('Europe/London')
print(datetime.now(tz).strftime('%a %d %b %Y %I:%M:%S.%f %p %z'))
print(datetime.now(tz).strftime('%a %d %b %Y %I:%M:%S.%f %p %Z'))
#Outputs
#Mon 07 Jun 2021 02:37:21.824101 PM +0100
#Mon 07 Jun 2021 02:37:21.824943 PM BST
tz = timezone('Europe/Paris')
print(datetime.now(tz).strftime('%a %d %b %Y %I:%M:%S.%f %p %z'))
print(datetime.now(tz).strftime('%a %d %b %Y %I:%M:%S.%f %p %Z'))
#Outputs
#Mon 07 Jun 2021 03:37:21.825939 PM +0200
#Mon 07 Jun 2021 03:37:21.825939 PM CEST
tz = timezone('Australia/Sydney')
print(datetime.now(tz).strftime('%a %d %b %Y %I:%M:%S.%f %p %z'))
print(datetime.now(tz).strftime('%a %d %b %Y %I:%M:%S.%f %p %Z'))
#Outputs
#Mon 07 Jun 2021 11:37:21.826935 PM +1000
#Mon 07 Jun 2021 11:37:21.826935 PM AEST
Note that using %Z vs %z format code will give a different output. Consult the datetime documentation for a full list of these formatting codes. You can get a full list of time zones HERE.
How Do I Set a Time Zone In pytz?
We have to set a timezone in cases where the DateTime value provided does not have Timezone information. We call this a NAIVE datetime value.
from datetime import datetime
from pytz import timezone
tz = timezone('America/Los_Angeles')
dt= datetime(2024,7,26)
print(dt.strftime('%z'))
#prints nothing because there is no timezone information
dt = tz.localize(dt).strftime('%z')
print(dt)
#outputs: -0700
Using localize() and an explicitly defined time zone we can convert NAIVE datetimes to time zone aware date times.
Conclusion
Be sure to include the pytz library in your projects where you need to cater for world time zones. This is a powerful tool to have indeed! Good luck. 👌👌👌.