Open In App

Handling timezone in Python

Improve
Improve
Like Article
Like
Save
Share
Report

There are some standard libraries we can use for timezones, here we’ll use pytz. This library has a timezone class for handling arbitrary fixed offsets from UTC and timezones.

Installation

pytz is a third-party package that you have to install. To install pytz use the following command –

pip install pytz

Getting Started

After installation import the pytz package and now let’s see how we can pass in the timezones. Basic syntax in Python to get the date and time information is :

datetime.datetime.now()

The above syntax returns the current local date-time without any timezone information. But with the use of the pytz package we can handle this date-time information in the various timezones –now() gives us the option to pass in a time zone, so if you leave the time zone empty then it will also return the current local date-time. The output of now() depends upon the machine. The local time and time zone settings of the host machine will determine the output.

So in order to work with the timezone smoothly, it is recommended to use the UTC as your base timezone. To get the Universal Time Coordinated i.e. UTC time we just pass in the parameter to now() function. To get the UTC time we can directly use the ‘pytz.utc’ as a parameter to now() function as ‘now(pytz.utc)’. The offset will be shown at the end as (+ or – hours).

The below code shows the local time of the machine and the UTC time with offset.

Example:

Python3




import datetime
import pytz
  
dtObject_local = datetime.datetime.now()
dtObject_utc = datetime.datetime.now(pytz.utc)
  
print(dtObject_local)
print(dtObject_utc)


Output:

2021-05-28 12:19:56.962055

2021-05-28 06:49:56.962055+00:00

As you can see now we got the date time info of the local machine and in UTC, the timezone offset at the end is +00:00.

If we want to get date time info of specific timezone we just need to add the timezone in the parameter –

Syntax:

.now(pytz.timezone(‘ YOUR_TIMEZONE ‘))

The parameter pytz.timezone allows us to specify the timezone information as a string. We can pass in any available timezone and will get the current date time of that timezone, and it will also print the offset with respect to the UTC. i.e., the difference between UTC timezone(+00:00) and the specified time zone.

Example: 

Python3




import datetime
import pytz
  
dtObject_utc = datetime.datetime.now(pytz.utc)
  
dtObject_asia = datetime.datetime.now(pytz.timezone('Asia/Kolkata'))
dtObject_usc = datetime.datetime.now(pytz.timezone('US/Central'))
dtObject_turkey = datetime.datetime.now(pytz.timezone('Turkey'))
dtObject_eoslo = datetime.datetime.now(pytz.timezone('Europe/Oslo'))
dtObject_abelem = datetime.datetime.now(pytz.timezone('America/Belem'))
  
print(dtObject_utc)
  
print(dtObject_asia)
print(dtObject_usc)
print(dtObject_turkey)
print(dtObject_eoslo)
print(dtObject_abelem)


Output:

2021-05-28 23:12:56.559011+00:00

2021-05-29 04:42:58.027452+05:30

2021-05-28 18:12:58.072254-05:00

2021-05-29 02:12:58.092257+03:00

2021-05-29 01:12:58.107930+02:00

2021-05-28 20:12:58.109932-03:00

Here we got the different date time according to different timezones with offset. The offset will also show the (+/-) hours.  We can also convert any time zone to a different time zone.

Converting between Timezones

astimezone() method is used to manipulate i.e., to convert the datetime objects to the new specified datetime object. It uses an instance of the datetime object and returns new timezone information.

Syntax:

astimezone(pytz.timezone(‘ NEW_TIMEZONE ‘))

To get exactly what we need the following syntax will be followed.

Syntax :

dtObj = datetimeObject_instance.astimezone(pytz.timezone(‘ NEW_TIMEZONE ‘))

where,

  • dtObj : which stores the returned datetime object with newly created time date
  • datetimeObject_instance : currently available datetime object (i.e., reference to the local date time)
  • NEW_TIMEZONE : timezone information which should be changed to

Example:

Python3




import datetime
import pytz
  
# getting our local timezone
dtObject_local = datetime.datetime.now()
  
# converting local timezone to 'US/Central'
dtObject_usc = dtObject_local.astimezone(pytz.timezone('US/Central'))
  
# now converting 'US/Central' timezone to 'Pacific/Chuuk'
dtObject_pchuuk = dtObject_usc.astimezone(pytz.timezone('Pacific/Chuuk'))
  
print(dtObject_local)
print(dtObject_usc)
print(dtObject_pchuuk)


Output:

2021-05-29 04:46:26.288233

2021-05-28 18:16:26.288233-05:00

2021-05-29 09:16:26.288233+10:00

pytz has a large list of timezones available you can print them using the following method.

Example:

Python3




import pytz
  
for timeZone in pytz.all_timezones:
    print(timeZone)


Output:

Africa/Abidjan

Africa/Accra

Africa/Addis_Ababa

Africa/Algiers

Africa/Asmara

.

.

.

US/Samoa

UTC

Universal

W-SU

WET

Zulu



Last Updated : 06 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads