Introduction

In Python, understanding timezones is especially important for working with datetime objects, which are widely used for storing dates and times in many applications. In this tutorial, we will cover the basics of timezones, including how to work with them using the built-in datetime module in Python. Whether you are a beginner or an experienced Python programmer, this tutorial will help you gain a better understanding of timezones in Python and improve your skills for working with dates and times in your projects.

Table of Contents :

  • Timezone in Python 
    • Creating a timezone aware datetime object using the timezone method
    • Creating a timezone aware datetime object using the replace method
    • Creating a timezone aware datetime object using the astimezone method
  • Get current time in different timezone
  • How to get timezone information using tzinfo
  • How to convert between timezones

Timezone in Python

  • A timezone is a region of the earth where the same standard time is used.
  • The  pytz  library is the recommended way of working with timezones in Python.
  • It provides access to the Olson timezone database.
  • We can create a timezone aware datetime object in python using the pytz library.
  • There are different ways of creating a timezone aware datetime object :
    • using the timezone method of  datetime  object.
    • using the replace method of  datetime  object
    • using the astimezone method of  datetime  object.

Creating a timezone aware datetime object using the timezone() method

  • We can use the  timezone()  method of datetime object to create a timezone aware datetime object.
  • The name of the timezone that we want to create is passed as an argument to the  timezone()  method.
  • Code Sample : 

import datetime
import pytz

local_zone = pytz.timezone('Europe/Warsaw')
local_time = datetime.datetime.now(local_zone)

print(local_time)

# Output
# 2022-07-15 15:17:06.790977+02:00



Creating a timezone aware datetime object using the replace method

  • We can create a timezone aware datetime object using the replace() method of datetime object.
  • The replace method takes a parameter  tzinfo .
  • It is the info about the new time zone that we want to create.
  • Code Sample : 

import datetime
import pytz

utc_now = datetime.datetime.now()
local_zone = pytz.timezone('Europe/Warsaw')
local_time = utc_now.replace(tzinfo=local_zone)

print("UTC Now : ", utc_now)
print("Local Time : ", local_time)


# Output
# UTC Now :  2020-07-15 18:40:06.865514
# Local Time :  2020-07-15 18:40:06.865514+01:24


Creating a timezone aware datetime object using the astimezone method

  • We can use the  astimezone()  method of datetime object to create a timezone aware datetime object.
  • The info about the timezone that we want to create is passed as an argument to the  astimezone()  method.
  • Code Sample : 

import datetime
import pytz

utc_time = datetime.datetime.utcnow()
utc_zone = pytz.utc
local_zone = pytz.timezone('Asia/Kolkata')
local_time = utc_time.astimezone(local_zone)

print("UTC Time : ", utc_time)
print("Local Time : ", local_time.strftime('%Y-%m-%d %H:%M:%S %Z%z'))

# Output
# UTC Time :  2020-07-15 13:14:43.367239
# Local Time :  2020-07-15 13:14:43 IST+0530


Get current time in different timezone

  • We have already seen in our previous section how we can create timezone aware datetime objects using the timezone() method of the datetime object.
  • To get the current time in different timezones : 
    • create different timezones using timezone() method
    • pass these timezones as an argument to the now() method one by one.
  • Code Sample : 

import datetime
import pytz

local_zone = pytz.timezone('Asia/Kolkata')
local_time = datetime.datetime.now(local_zone)
utc_zone = pytz.utc
utc_time = local_time.astimezone(utc_zone)

print(f"Current Time in Asia/Kolkata: {local_time}")
print(f"Current Time in UTC: {utc_time}")

# Output
# Current Time in Asia/Kolkata: 2021-07-21 18:54:15.499901+05:30
# Current Time in UTC: 2021-07-21 13:24:15.499901+00:00



How to get timezone information using tzinfo

  • We can get the info of any timezone using tzinfo.
  • Code Sample : 

import datetime
import pytz

local_zone = pytz.timezone('Asia/Kolkata')
local_time = datetime.datetime.now(local_zone)
tzinfo = local_time.tzinfo

print(tzinfo)

# Output
# Asia/Kolkata



How to convert between timezones

  • We have already read about the astimezone() method.
  • We can use this method to convert time between different timezones.
  • Code Sample : 

import datetime
import pytz

local_zone_1 = pytz.timezone('Europe/Warsaw')
local_zone_2 = pytz.timezone('America/New_York')
local_time_1 = datetime.datetime.now(local_zone_1)
local_time_2 = local_time_1.astimezone(local_zone_2)

print(f"Time in Asia/Kolkata: {local_time_1}")
print(f"Time in America/New_York: {local_time_2}")

# Output
# Time in Asia/Kolkata: 2023-07-15 15:36:28.855026+02:00
# Time in America/New_York: 2023-07-15 09:36:28.855026-04:00




Prev. Tutorial : Using python time.sleep()

Next Tutorial : Intro to file handling