PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Python DateTime » Working With TimeZones in Python

Working With TimeZones in Python

Updated on: December 5, 2021 | 3 Comments

In this tutorial, we will learn how to work with timezone in Python.

After reading this article, you’ll learn:

  • Handling of timezone by creating a timezone aware date and time.
  • How to get the current time in a different timezone
  • Get the current timezone name, UTC offset, and DST offset if DST is in effect
  • How to convert UTC Datetime to a specific timezone
  • Convert time from one timezone to another
  • Mathematical operations on two timezone-aware and timezone naive objects
  • Learn the tzinfo implementation modules namely pytz, ZoneInfo, and their methods.

Table of contents

  • What is timezone in Python?
  • Create Timezone Aware Datetime Object
    • Create TimeZone Aware Datetime Object Using timezone class
  • Get Current Time in Different Timezone
  • Get TimeZone Information Using tzinfo
  • Converting Between Timezones
  • Working with Local Timezones

What is timezone in Python?

A time zone represents the standardized time depending on which part of the world is being considered.

In simple terms, timezone refers to the local time of a region. UTC (Coordinated Universal Time) is the astronomical time based on earth’s rotation, is the standard against which the world’s region-based time is coordinated.

Note: UTC – Coordinated Universal Time is the common time standard across the world. So, in Python, to work with the timezone without any issues, it is recommended to use the UTC as your base timezone

For example, CT(Central Time) in North and South America is either 5 or 6 hours behind and represented as UTC-5 or UTC-6 based on the Day Light Saving. Below are a few examples.

UTC OffsetLocationsNameLocation
UTC +9Japan, South Korea, and 5 moreJSTTokyo
UTC +5:30IndiaISTIndia
UTC +1The United Kingdom and 20 moreBSTLondon
UTC -10Hawaii/USA and 2 moreHSTHonolulu
TimeZones

Python provides the datetime.tzinfo abstract base class which provides methods to handle timezone. But this class is an abstract base class and should not be instantiated directly. We need to define a subclass of tzinfo to capture information about a particular time zone.

The pytz library has implemented a timezone class for handling arbitrary fixed offsets from UTC and timezones. This library allows accurate and cross-platform timezone calculations and also solves the issue of ambiguous times at the end of daylight saving time.

pytz is a concrete implementation of the abstract base class tzinfo and is used to create timezone-aware datetime objects.

For example, The datetime.now() function returns the current local date-time without any timezone information. Using the pytz library, we can pass the timezone name to this function to get the current datetime in the given timezone.

We’ll use the following attributes and methods of the pytz module to work with timezone in Python.

  • pytz.utc: Get the standard UTC timezone
  • pytz.timezone('region'): Create the timezone object of a particular region
  • pytz.astimezone(): Convert the time of a particular time zone into another time zone

Create Timezone Aware Datetime Object

In Python, a date object can be mentioned with or without timezones. Based on that, an object is known as Naive or Aware. A date object, by default, is naive. A datetime or time object is aware if it holds the timezone(tz) value.

Follow the below steps to create a timezone aware Datetime Object in Python: –

  • Install pytz module if not installed using the pip install pytz command.
  • Use the pytz.timezone('region_name') function to create the timezone object
  • Use the datetime.now(timezone_obj) or datetime.datetime('timezone') function to create the timezone aware current datetime.