Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python pytz Module

Python pytz module serves the date-time conversion functionalities. Thus, enables users serving international client’s base.

The pytz module enables time-zone calculations in our Python applications.

Installation of pytz module:

pip install pytz

Pytz Installation 1
Python pytz Installation

Python pytz Module Functions

  • all_timezones
  • all_timezones_set
  • common_timezones
  • common_timezones_set
  • Fetching time of a given time-zone
  • country_names
  • country_timezones

1. all_timezones

This function returns a list of all the time-zones supported by Python’s pytz module.

Example:

import pytz

print('The timezones supported by pytz module: ', pytz.all_timezones, '\n')

Output Snippet:

The timezones supported by pytz module:  ['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', 'Africa/Asmara', 'Africa/Asmera', 'Africa/Bamako', 'Africa/Bangui', 'Africa/Banjul']

We have shown only a few values because the actual list is very long.


2. all_timezones_set

This variable returns a set of all the supported timezones.

Example:

import pytz
print('List of all the supported timezones: ', pytz.all_timezones_set, '\n')

Output Snippet:

List of all the supported timezones:  LazySet({'America/Port-au-Prince', 'Asia/Ust-Nera', 'Asia/Vientiane', 'Australia/Hobart', 'Asia/Ulaanbaatar', 'Africa/Lome'})

3. common_timezones

This variable returns a list of commonly used time-zones.

Example:

import pytz

print('Commonly used time-zones: ', pytz.common_timezones, '\n')

Output Snippet:

Commonly used time-zones:  ['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', 'Africa/Asmara', 'Africa/Bamako', 'Africa/Bangui', 'Africa/Banjul', 'US/Pacific', 'UTC']

4. common_timezones_set

import pytz
print('common_timezones_set() = ', pytz.common_timezones_set, '\n')

Output:

common_timezones_set() =  LazySet({'America/Eirunepe', 'Africa/Kinshasa', 'Australia/Sydney', 'Europe/Malta', 'America/Tortola', 'Canada/Pacific', 'America/Argentina/Cordoba'})

5. Fetching time of a given time-zone

The pytz.timezone() returns the timezone object by the name. And, the datetime.now() returns the date-time of that particular time-zone.

from datetime import datetime
import pytz
# getting utc timezone
utc_time = pytz.utc

# getting timezone by name
ist_time = pytz.timezone('Asia/Kolkata')

# getting datetime of specified timezone
print('Datetime of UTC Time-zone: ', datetime.now(tz=utc_time))
print('Datetime of IST Time-zone: ', datetime.now(tz=ist_time))

Output:

Datetime of UTC Time-zone:  2020-01-03 17:49:14.220167+00:00
Datetime of IST Time-zone:  2020-01-03 23:19:14.220167+05:30

6. country_names

The country_names returns a dictionary of country ISO Alpha-2 Code and country name as a key-value pair.

import pytz

print('country_names =')
for key, val in pytz.country_names.items():
    print(key, '=', val, end=',')
print('\n')
print('Country name equivalent to the input country code: ', pytz.country_names['AQ'])

Output:

country_names =
AD = Andorra,AE = United Arab Emirates,AF = Afghanistan,AG = Antigua & Barbuda,AI = Anguilla,AL = Albania,AM = Armenia,AO = Angola,AQ = Antarctica,ZW = Zimbabwe,
Country name equivalent to the input country code:  Antarctica

7. country_timezones

This function provides a dictionary of country ISO Alpha-2 Code as key and list of supported time-zones for a particular input key (country code) as output.

import pytz
print('country_timezones =')
for key, val in pytz.country_timezones.items():
    print(key, '=', val, end=',')
print('\n')
print('Time-zones supported by Antartica =', pytz.country_timezones['AQ'])

Output:

country_timezones =
AD = ['Europe/Andorra'],AE = ['Asia/Dubai'],AF = ['Asia/Kabul'],AG = ['America/Antigua'],AI = ['America/Anguilla'],AL = ['Europe/Tirane'],AM = ['Asia/Yerevan'],AO = ['Africa/Luanda'],ZW = ['Africa/Harare'],
Time-zones supported by Antartica = ['Antarctica/McMurdo', 'Antarctica/Casey', 'Antarctica/Davis', 'Antarctica/DumontDUrville', 'Antarctica/Mawson', 'Antarctica/Palmer']

References