A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-pytz/ below:

Python pytz - GeeksforGeeks

Python pytz

Last Updated : 10 May, 2025

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

Using setuptools:

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))

Output
2025-05-09 10:15:40 UTC+0000
2025-05-09 15:45:40 IST+0530

Explanation:

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:

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_timezones

Explanation:

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))

Output
UTC Time = 2025-05-09 10:57:48.768744+00:00
IST Time = 2025-05-09 13:57:48.768794+03:00

Explanation:

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)

Output
January 10, 1984
1984-01-10T23:30:00

Explanation:

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'))

Output
IST Current Time = 2025-05-09 11:01:04 IST+0530
Wrong UTC Current Time = 2025-05-09 11:01:04 UTC+0000

Explanation:

Example2:

Python
from datetime import datetime
from pytz import timezone

naive = datetime(2019, 8, 19, 12, 0)

aware = timezone('UTC').localize(naive)
print(aware)

Output
2019-08-19 12:00:00+00:00

Explanation:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4