Python datetime.astimezone() Method

Python datetime.astimezone() Method

  1. Syntax of Python datetime.astimezone() Method
  2. Example 1: Use astimezone With tz Module
  3. Example 2: Use astimezone With datetime.timezone

The datetime module is a built-in module available in Python programming language. This library offers reliable and efficient functions to work with dates, times, time differences, and time zones and perform various calculations over them.

This module contains a class datetime that stores date along with time and timezone. This class’s objects have access to different instance methods such as astimezone() that return a new datetime object with timezone information.

Syntax of Python datetime.astimezone() Method

<datetime object>.astimezone()

Parameters

Type Description
tz tzinfo A tzinfo object representing a valid timezone. The default value is None.

The tzinfo is an abstract base class that stores details about a timezone. Since it is an abstract class, it cannot be instantiated, and some concrete class has to instantiate the tzinfo class and implement its four important methods: utcoffset(dt), dst(dt), tzname(dt), and fromutc(dt).

In this article, we will not perform the actual implementation of the concrete class. Instead, we will use the tz module that has already subclassed the abstract tzinfo class and the timezone class of the datetime module.

To learn more about the tzinfo, its methods, and how it should be inherited and implemented, refer to the official Python documentation here. To learn more about the tz module in-depth, refer to the official documentation here.

Returns

This method returns a new datetime object with a new tz attribute of type tzinfo. The date and time values are adjusted to the local time related to the timezone.

By default, the datetime object stores the date and time according to UTC or Coordinated Universal Time. In real-world applications, date and time are stored in UTC and not hard-coded in the server’s or user’s timezone.

Depending upon the user’s location and timezone, date and time values are adjusted and presented to the user. And it’s very straightforward to convert date and time from one timezone to another.

Example 1: Use astimezone With tz Module

from datetime import datetime
from dateutil import tz

dt = datetime.now()
t1 = tz.gettz("America/Los_Angeles")
t2 = tz.gettz("Asia/Kolkata")
t3 = tz.gettz("Asia/Singapore")
t4 = tz.gettz("Europe/Paris")
t5 = tz.gettz("Canada/Pacific")
print("UTC:", dt)
print("America/Los_Angeles:", dt.astimezone(t1))
print("Asia/Kolkata:", dt.astimezone(t2))
print("Asia/Singapore:", dt.astimezone(t3))
print("Europe/Paris:", dt.astimezone(t4))
print("Canada/Pacific:", dt.astimezone(t5))

Output:

UTC: 2022-08-24 12:30:45
America/Los_Angeles: 2022-08-24 05:30:45-07:00
Asia/Kolkata: 2022-08-24 18:00:45+05:30
Asia/Singapore: 2022-08-24 20:30:45+08:00
Europe/Paris: 2022-08-24 14:30:45+02:00
Canada/Pacific: 2022-08-24 05:30:45-07:00

The Python code above first creates a datetime object for the current time. Next, it creates 5 objects representing the timezones, namely, America/Los_Angeles, Asia/Kolkata, Asia/Singapore, Europe/Paris, and Canada/Pacific.

All these objects are of type <class 'dateutil.zoneinfo.tzfile'>. One can verify that using Python’s built-in type() method.

The tzfile is the concrete class that inherits the abstract class tzinfo and implements all the crucial methods. Next, using the astimezone() method, 5 timezone-aware datetime objects are created.

Let’s compare all the new 5 datetime objects with the basic datetime object (UTC timezone). We will find that for every timezone-aware object, the date and time are adjusted to the timezone’s local time.

Depending on the timezone sign, we can easily verify that by adding or subtracting time from the UTC time.

For example, in the case of America/Los_Angeles, we’ll subtract 7 hours (-07:00) from the UTC and in the case of Asia/Kolkata, we will add 5 hours and 30 minutes (+05:30) to the UTC to get the local time.

Example 2: Use astimezone With datetime.timezone

from datetime import datetime, timezone, timedelta

dt = datetime.now()
t1 = timezone(timedelta(hours = -7, minutes = 0), "America/Los_Angeles")
t2 = timezone(timedelta(hours = 5, minutes = 30), "Asia/Kolkata")
t3 = timezone(timedelta(hours = 8, minutes = 0), "Asia/Singapore")
t4 = timezone(timedelta(hours = 2, minutes = 0), "Europe/Paris")
t5 = timezone(timedelta(hours = 7, minutes = 0), "Canada/Pacific")
print("UTC:", dt)
print("America/Los_Angeles:", dt.astimezone(t1))
print("Asia/Kolkata:", dt.astimezone(t2))
print("Asia/Singapore:", dt.astimezone(t3))
print("Europe/Paris:", dt.astimezone(t4))
print("Canada/Pacific:", dt.astimezone(t5))

Output:

UTC: 2022-08-24 12:34:33.325235
America/Los_Angeles: 2022-08-24 05:34:33.325235-07:00
Asia/Kolkata: 2022-08-24 18:04:33.325235+05:30
Asia/Singapore: 2022-08-24 20:34:33.325235+08:00
Europe/Paris: 2022-08-24 14:34:33.325235+02:00
Canada/Pacific: 2022-08-24 05:34:33.325235-07:00

The datetime.timezone class represents a timezone differently. It uses datetime.timedelta to represent the offset.

Apart from that, everything remains the same as in the previous example.

Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

LinkedIn GitHub

Related Article - Python Datetime

  • Python datetime.date.date.fromtimestamp() Method
  • Python datetime.date.date.replace() Method
  • Python datetime.date.date.toordinal() Method
  • Python datetime.datetime.timetuple() Method
  • Python datetime.time.replace() Method
  • Python datetime.time.time.strftime() Method