Python datetime.tzinfo Class

Python datetime.tzinfo Class

  1. the tzinfo Class in Python
  2. Syntax of Python tzinfo Class
  3. Example Codes: Use Different Methods of datetime.timezone Class in Python

The datetime module offers several methods and classes to work with dates and times, such as date, timedelta, datetime, tzinfo, and timezone. These classes are enriched with numerous functions and attributes that make working with them flawless.

This article will discuss the tzinfo class in detail.

the tzinfo Class in Python

The tzinfo is an abstract class available in the os module that contains four methods, namely, utcoffset(dt), dst(dt), tzname(dt), and fromutc(dt). The objects of the class inheriting the abstract tzinfo class can be used in datetime and time class objects to create timezone aware datetime and time instances.

In a timezone-aware object, values are altered based on its time zone. Note that datetime and time objects need some methods implemented in the timezone object.

The os module has a concrete class timezone that inherits the tzinfo class and implements all these methods. Hence, it can be used to create timezone-aware datetime and time objects and note that one can create a class that extends the tzinfo class and represents timezones.

Syntax of Python tzinfo Class



Since it is an abstract class, it doesn’t accept any parameters.


Since it is an abstract class, it doesn’t return anything. Additionally, one has to extend this class and implement all the abstract methods to use this class.


Method Parameters Returns
utcoffset(dt) dt is a datetime object. It returns a timedelta object representing an offset of local time from UTC. If the local time is east of UTC, it is positive (India Standard Standard is UTC+05:30), and if it is west of UTC, it is negative (Atlantic Standard Time is UTC-04:00).
dst(dt) dt is a datetime object. It returns a timedelta object representing daylight saving time or DST adjustment. If DST information is not known, it returns None.
tzname(dt) dt is a datetime object. It returns the time zone’s name associated with the dt object as a string.
fromutc(dt) dt is a datetime object. It returns a datetime object with adjusted date and time values according to the local time.

Example Codes: Use Different Methods of datetime.timezone Class in Python

The datetime.timezone class is a concrete class that inherits the tzinfo class.

import datetime

dt = datetime.datetime(2022, 9, 10, 1, 21, 32, 676435)
tm = datetime.timezone(datetime.timedelta(hours = 2), name = "CET")
dt = dt.astimezone(tm)
print("CET:", dt)
print("UTC Offset:", tm.utcoffset(None))
print("Name of the Timezone:", tm.tzname(None))
print("DST: ", tm.dst(None))
print("From UTC:", tm.fromutc(dt))


CET: 2022-09-10 03:21:32.676435+02:00
UTC Offset: 2:00:00
Name of the Timezone: CET
DST: None
From UTC: 2022-09-10 05:21:32.676435+02:00

The code above first creates a datetime object and declares a timezone using the timezone class. We declare the CET, which is UTC+02:00.

Next, using the astimezone() available on a datetime object, we create a timezone-aware datetime object. We use all four methods that the tzinfo abstract class contains and were implemented in the timezone class.

Since there are no details about the daylight cycle, the dst() method returns None.

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.datetime.year Attribute
  • Python datetime.datetime.replace() Method
  • Python datetime.timezone Class
  • Python Datetime.fromtimestamp() Method
  • Python Datetime.timedelta() Method