The pytz
module brings the Olson timezone database into Python and enables accurate and timezone-aware datetime operations. It is especially useful in applications that serve a global user base, allowing you to convert and manipulate datetimes across different time zones reliably.
Installation
You can install the pytz module using any of the following methods:
Using pip:
pip install pytz
Using tarball:
python setup.py install
easy_install --upgrade pytz
Converting timezones
The astimezone() function is used to convert a timezone-aware datetime object from one timezone to another.
Syntax:
datetime_obj.astimezone(target_timezone)
Example:
Python
from datetime import datetime
from pytz import timezone
fmt = "%Y-%m-%d %H:%M:%S %Z%z"
# Get current UTC time
utc_now = datetime.now(timezone('UTC'))
print(utc_now.strftime(fmt))
# Convert to Asia/Kolkata timezone
ist_now = utc_now.astimezone(timezone('Asia/Kolkata'))
print(ist_now.strftime(fmt))
Output2025-05-09 10:15:40 UTC+0000
2025-05-09 15:45:40 IST+0530
Explanation:
- datetime.now(timezone('UTC')): Gets the current time in UTC with timezone awareness.
- astimezone(timezone('Asia/Kolkata')): Converts the UTC time to Asia/Kolkata (IST) timezone.
- strftime(fmt): Formats the datetime into a readable string showing date, time, timezone abbreviation, and offset.
Python pytz attributes
There are some attributes in pytz module to help us find the supported timezone strings. These attributes will help understand this module better.
1. all_timezones
It returns the list all the available timezones with pytz.all_timezones:
Python
import pytz
print('the supported timezones by the pytz module:', pytz.all_timezones, '\n')
Output:

Explanation: This code imports the pytz module and prints all the supported timezones using pytz.all_timezones, which returns a list of timezone names available in the module.
2. all_timezones_set
It returns the set of all the available timezones with pytz.all_timezones_set:
Python
import pytz
print('all the supported timezones set:', pytz.all_timezones_set, '\n')
Output

Explanation: This code prints all the supported timezones as a set using pytz.all_timezones_set, which provides the same timezones as all_timezones but in an unordered, unique set format.
3. Common_timezones, Common_timezones_set
It returns the list and set of commonly used timezones with pytz.common_timezones, pytz.common_timezones_set.
Python
import pytz
print('Commonly used time-zones:',
pytz.common_timezones, '\n')
print('Commonly used time-zones-set:',
pytz.common_timezones_set, '\n')
Output

Explanation:
- pytz.common_timezones: returns a list of frequently used timezone names.
- pytz.common_timezones_set: returns the same timezones as an unordered set.
4. country_names
It returns a dict of country ISO Alpha-2 Code and country name as a key-value pair.
Python
import pytz
print('country_names =')
for key, val in pytz.country_names.items():
print(key, '=', val, end=',')
print('\n')
print('equivalent country name to the input code: =', pytz.country_names['IN'])
Output
country_names Explanation: This code prints a dictionary of country ISO codes and their country names using pytz.country_names. It also retrieves the country name for the code 'IN', which corresponds to India.
5. country_timezones
It returns a dictionary of country ISO Alpha-2 Code as key and list of supported time-zones for a particular input key (country code) as value
Python
import pytz
print('country_timezones =')
for key, val in pytz.country_timezones.items():
print(key, '=', val, end=',')
print('\n')
print('Time-zones supported by Antarctica =', pytz.country_timezones['AQ'])
Output
country_timezonesExplanation:
- pytz.country_timezones.items() lists each country code with its associated timezones.
- pytz.country_timezones['AQ'] specifically shows all the timezones used in Antarctica.
Python pytz example
Example 1: creating datetime instance with timezone information
This code demonstrates how to get the current time in different timezones using the pytz module in Python.
Python
import pytz
import datetime
from datetime import datetime
utc = pytz.utc
kiev_tz = pytz.timezone('Europe/Kiev')
print('UTC Time =', datetime.now(tz=utc))
print('IST Time =', datetime.now(tz=kiev_tz))
OutputUTC Time = 2025-05-09 10:57:48.768744+00:00
IST Time = 2025-05-09 13:57:48.768794+03:00
Explanation:
- pytz.utc: Retrieves the UTC timezone object.
- pytz.timezone('Europe/Kiev'): Loads the timezone for Kiev, Ukraine.
- datetime.now(tz=...): Returns the current time localized to the given timezone (UTC and Kiev in this case).
- The output shows both UTC time and Kiev time with their respective timezone offsets.
Example 2: Formatting Datetime
This code shows how to format a datetime object into human-readable and ISO 8601 string formats using Python's datetime module.
Python
import pytz
import datetime
d = datetime.datetime(1984, 1, 10, 23, 30)
d1 = d.strftime("%B %d, %Y")
d2 = d.isoformat()
print(d1)
print(d2)
OutputJanuary 10, 1984
1984-01-10T23:30:00
Explanation:
- d = datetime.datetime(1984, 1, 10, 23, 30): Creates a naive datetime object for January 10, 1984, at 11:30 PM.
- d.strftime("%B %d, %Y"): Formats the date as a readable string like "January 10, 1984".
- d.isoformat(): Converts the datetime to ISO 8601 format: "1984-01-10T23:30:00".
- The two formats are printed to show different representations of the same datetime.
Using localize() to Make Naive Datetime Timezone-Aware
The localize() function is used to assign a timezone to a naive datetime object. This is crucial when you receive a datetime input without timezone info and need to assign one correctly.
Example1:
Python
import pytz
import datetime
from datetime import datetime
ist = pytz.timezone('Asia/Kolkata')
utc = pytz.utc
local_datetime = ist.localize(datetime.now())
print('IST Current Time =', local_datetime.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
print('Wrong UTC Current Time =', utc.localize(
datetime.now()).strftime('%Y-%m-%d %H:%M:%S %Z%z'))
OutputIST Current Time = 2025-05-09 11:01:04 IST+0530
Wrong UTC Current Time = 2025-05-09 11:01:04 UTC+0000
Explanation:
- datetime.now() creates a naive datetime (without timezone info).
- ist.localize(datetime.now()): Converts the current naive datetime to IST (Asia/Kolkata).
- utc.localize(datetime.now()): Converts a separate naive datetime to UTC.
Example2:
Python
from datetime import datetime
from pytz import timezone
naive = datetime(2019, 8, 19, 12, 0)
aware = timezone('UTC').localize(naive)
print(aware)
Output2019-08-19 12:00:00+00:00
Explanation:
- datetime(2019, 8, 19, 12, 0): Creates a naive datetime for August 19, 2019 at 12:00 PM.
- timezone('UTC').localize(naive): Converts the naive datetime to UTC timezone-aware using pytz.localize().
- print(aware): Displays the datetime with timezone info → 2019-08-19 12:00:00+00:00.
Similar Reads
Python Quiz These Python quiz questions are designed to help you become more familiar with Python and test your knowledge across various topics. From Python basics to advanced concepts, these topic-specific quizzes offer a comprehensive way to practice and assess your understanding of Python concepts. These Pyt
3 min read
Python vs Cpython Python is a high-level, interpreted programming language favored for its readability and versatility. It's widely used in web development, data science, machine learning, scripting, and more. However, Cpython is the default and most widely used implementation of the Python language. It's written in
4 min read
Python Syllabus Hereâs a straight-to-the-point breakdown of what a python course covers from the basics to advanced concepts like data handling, automation and object-oriented programming. No fluff, just the essentials to get you coding fast.Getting Started with Python ProgrammingWelcome to the getting started with
6 min read
Python 101 Welcome to "Python 101," your comprehensive guide to understanding and mastering the fundamentals of Python programming. Python is a versatile and powerful high-level programming language that has gained immense popularity due to its simplicity and readability. Whether you're an aspiring programmer,
13 min read
What's the Zen of Python? Whether you come from a strong programming background or are just a beginner, you must be aware of Python programming language because of its attractive syntax, ease of reading, and simplicity; which most developers use across the globe today. A collection of aphorisms known as "The Zen of Python" e
9 min read
Python A-Z Quick Notes Python is a general-purpose, high-level, interpreted programming language that supports multiple programming paradigms, including procedural, object-oriented, and functional programming, and is widely used among the developersâ community. Python was mainly developed for emphasis on code readability,
15+ min read