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?
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 Offset | Locations | Name | Location |
|---|---|---|---|
| UTC +9 | Japan, South Korea, and 5 more | JST | Tokyo |
| UTC +5:30 | India | IST | India |
| UTC +1 | The United Kingdom and 20 more | BST | London |
| UTC -10 | Hawaii/USA and 2 more | HST | Honolulu |
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 timezonepytz.timezone('region'): Create the timezone object of a particular regionpytz.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 pytzcommand. - Use the
pytz.timezone('region_name')function to create the timezone object - Use the
datetime.now(timezone_obj)ordatetime.datetime('timezone')function to create the timezone aware current datetime.