Python DateTime.tzinfo()

The datetime.now() function contains no information regarding time zones. It only makes use of the current system time. Tzinfo is an abstract base class in Python. It cannot be directly instantiated. A concrete subclass must derive from this abstract class and implement the methods offered by it. 

List of Python DateTime.tzinfo() Objects

Function Name

Description

dst() Returns tzinfo.dst() is tzinfo is not None
fromutc() The purpose of this function is to adjust the date time data, 
returning an equivalent DateTime in self’s local time.
tzname() Returns tzinfo.tzname() is tzinfo is not None
utcoffset() Returns tzinfo.utcffsets() is tzinfo is not None

Example 

The tzinfo class instance can be provided to the DateTime and time object constructors. It is used in scenarios such as converting local time to UTC or accounting for daylight savings time.

Python3




import datetime as dt
from dateutil import tz
 
 
tz_string = dt.datetime.now(dt.timezone.utc).astimezone().tzname()
 
print("datetime.now() :", tz_string)
 
NYC = tz.gettz('Europe / Berlin')
dt1 = dt.datetime(2022, 5, 21, 12, 0)
dt2 = dt.datetime(2022, 12, 21, 12, 0, tzinfo=NYC)
 
print("Naive Object :", dt1.tzname())
print("Aware Object :", dt2.tzname())


Output:

datetime.now() : IST
Naive Object : None
Aware Object : None

Python datetime module

In Python, date and time are not data types of their own, but a module named DateTime in Python can be imported to work with the date as well as time. Python Datetime module comes built into Python, so there is no need to install it externally. 

In this article, we will explore How DateTime in Python works and what are the main classes of DateTime module in Python.

Table of Content

  • Python DateTime module
  • Python Date Class
  • Python Time class
  • Python Datetime class
  • Python Timedelta Class
  • Python DateTime.tzinfo()
  • Python DateTime timezone

Similar Reads

Python DateTime module

Python Datetime module supplies classes to work with date and time. These classes provide several functions to deal with dates, times, and time intervals. Date and DateTime are an object in Python, so when you manipulate them, you are manipulating objects and not strings or timestamps....

Python Date Class

The date class is used to instantiate date objects in Python. When an object of this class is instantiated, it represents a date in the format YYYY-MM-DD. The constructor of this class needs three mandatory arguments year, month, and date....

Python Time class

...

Python Datetime class

...

Python Timedelta Class

...

Python DateTime.tzinfo()

...

Python DateTime timezone

...

Contact Us