Disentangling Python Timezone | Pytz Explained

Calculating timezones using code can be a little tricky considering all the 24 different types of timezones. There are instances where you will need to convert from one timezone to another or display time. Python solves this problem by providing modules for managing time zones.

About the Module

“Date/Time” is not a type in Python on its own. Hence, the module pytz. This allows us to work with time as a “time” object. Python 3.10 does not include this module in its standard library. It’s manually installed.

Installing + Importing the Module

This package can either be installed using PIP or from a tarball using the standard Python distutils.

PIP Installation
pip install pytz
Tarball Installation
python setup.py install
Importing the Module
import pytz

About the function

The default timezone set for the function is UTC.

pytz.timezone('TZabbreivation') 

Converting a Date to a specific Time Zone using .timezone()

Let’s see how we can convert a date to a specific time zone.

import pytz
from datetime import datetime #creating a date object using Python datetime

# retrieving required timezone
coloradoTZ = pytz.timezone('America/Denver')

# converting to time using mentioned timezone
time_coloradoTZ = datetime.now(tz = coloradoTZ)
print(time_coloradoTZ)

Output

2022-03-21 13:22:39.791671+05:30

.timezone() from Python datetime module

About Python datetime

.timezone() is also present in Python datetime. datetime is a part of the Python Standard Library present under the Data Types category. Python datetime module provides us classes to work with date & time. These classes provide functions to deal with dates, times, and time intervals. date and datetime are considered as objects in Python. By manipulating them, you are actually manipulating datetime objects, not strings or timestamps.

Other Functions from the Module

Apart from .timezone(), make sure you know what these functions do as well.

Viewing all Available Timezones Using .all_timezones()

You can display all existing time zones by running:

import pytz
allTZ = pytz.all_timezones
print(allTZ)

This provides us with the time zones in a list.

Sample Output

['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', 'Africa/Asmara', 'Africa/Asmera', 'Africa/Bamako', 'Africa/Bangui', 'Africa/Banjul', 'Africa/Bissau', 'Africa/Blantyre', 'Africa/Brazzaville', 'Africa/Bujumbura', 'Africa/Cairo', 'Africa/Casablanca', 'Africa/Ceuta', 'Africa/Conakry', 'Africa/Dakar', 'Africa/Dar_es_Salaam', 'Africa/Djibouti', 'Africa/Douala', 'Africa/El_Aaiun', 'Africa/Freetown', 'Africa/Gaborone', 'Africa/Harare', 'Africa/Johannesburg', 'Africa/Juba', 'Africa/Kampala', 'Africa/Khartoum', 'Africa/Kigali', 'Africa/Kinshasa', 'Africa/Lagos', 'Africa/Libreville', 'Africa/Lome', 'Africa/Luanda', 'Africa/Lubumbashi', 'Africa/Lusaka', 'Africa/Malabo', 'Africa/Maputo', 'Africa/Maseru', etc.]

NOTE: This is a sample of a much larger output. For the complete result, try running the command on our online compiler.

Viewing Common Time Zones Using .common_timezones()

Display commonly used time zones by running this function.

import pytz
commonTZ = pytz.common_timezones
print(commonTZ)

Sample Output

['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', 'Africa/Asmara', 'Africa/Bamako', 'Africa/Bangui', 'Africa/Banjul', 'Africa/Bissau', 'Africa/Blantyre', 'Africa/Brazzaville', 'Africa/Bujumbura', 'Africa/Cairo', 'Africa/Casablanca', 'Africa/Ceuta', 'Africa/Conakry', 'Africa/Dakar', 'Africa/Dar_es_Salaam', 'Africa/Djibouti', 'Africa/Douala', 'Africa/El_Aaiun', 'Africa/Freetown', 'Africa/Gaborone', 'Africa/Harare', 'Africa/Johannesburg', 'Africa/Juba', 'Africa/Kampala', 'Africa/Khartoum', 'Africa/Kigali', 'Africa/Kinshasa', etc. ]

NOTE: This is a sample of a much larger output. For the complete result, try running the command on our online compiler.

Python Timezones in Django

The Django database stores date-time information in UTC. It uses time-zone-aware date-time objects internally and translates them to the end user’s local time zone in templates and forms.

It’s good practice to store data in UTC in your database as most countries follow DST. If you’re working in local time, you’re likely to encounter errors twice a year when the transitions happen.

Time zone support is disabled by default. To enable it, set USE_TZ = True in your settings file.

How to get a Python timezone offset

The following demonstration will show how we can retrieve the offset for a particular timezone.

import pytz
import datetime
# Creating Timezone object
greenwich = pytz.timezone('GMT')

# Setting date + time
dateTime= datetime.datetime(2022, 10, 31, 2, 12, 30)

# Retrieving offset while taking day light saving time into consideration
offset = cet.utcoffset(dateTime, is_dst = True)

pytz.localize vs datetime.replace

pytz.localize()datetime.replace()
For creating “date-time” aware objects with an initial fixed “date-time” value, we can use localize()..replace() Is used to replace the contents of the “date-time” object with the given parameters.
The resulting “date-time” aware object will have the original “date-time” value.Internally, UTC can be used as the time zone and replace can be used to perform I/O operations of date-times in a localized way.

pytz.localize vs datetime.astimezone

pytz.localize()datetime.astimezone()
For creating “date-time” aware objects with an initial fixed “date-time” value, we can use localize().astimezone() method is used to manipulate objects of the DateTime class of module Datetime. It uses an instance of the class and returns an Datetime object with the new tzinfo attribute tz. It works on a datetime class instance.
It is part of an external module pytz.It is part of a Python Standard Module datetime. It comes under the Data Types category.

FAQs on Python Timezone

Whats the difference between .all_timezones() vs .all_timezones_set()

Both functions display the available time zones from the module.
The only difference being is that .all_timezones() returns a list and .all_timezones_set() returns a set.

Is “pytz” better than Python datetime?

Offsets from UTC are rounded to the nearest whole minute. Time zones such as Europe/Amsterdam pre-1937 will be up to 30 seconds out. This is a limitation of the Python datetime library.

How to convert Python timezone string to tzinfo?

This can be done using the pytz module. By passing the timezone string into, we can create a date-time object. We can then pass the object into datetime.datetime.now(), which provides the current date and time.

Conclusion

Our focus has been on the pytz.timezone(). We have gone through how the module and its functions come together to create and manipulate ‘date-time’ objects. We examined datetime.timezone() and how it works differently from the function we discussed.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments