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
Contact Us