A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-datetime-tzinfo/ below:

datetime.tzinfo() in Python - GeeksforGeeks

datetime.tzinfo() in Python

Last Updated : 12 Jul, 2025

datetime.tzinfo() class is an abstract base class in Python that provides the interface for working with time zone information. The tzinfo class itself does not store any time zone data; instead, it is intended to be subclassed. The subclass can provide the logic to convert datetime objects to and from different time zones.

Methods of tzinfo

The methods available for implementation in tzinfo base class are :

1. utcoffset(self, dt)

This method should return the offset of the time zone (in minutes) from UTC for a given datetime object dt. Example: For a UTC+5 hour time zone, this method will return a timedelta object representing 5 hours.

Python
from datetime import timedelta, tzinfo

class Mytz(tzinfo):
    def utcoffset(self, dt):
        return timedelta(hours=5)  

# Example usage
tz = Mytz()
print("UTC Offset:", tz.utcoffset(None))  

Output
UTC Offset: 5:00:00

Explanation: In the example, the method is defined to return timedelta(hours=5), which means the time zone is UTC +5 hours. The utcoffset() method is used to get the time difference from UTC for a given date-time object.

2. dst(self, dt)

dst() method returns the daylight saving time (DST) adjustment. If the time zone observes daylight saving, it returns a timedelta representing the shift during DST; otherwise, it returns None or timedelta(0).

Python
from datetime import timedelta, tzinfo

class Mytz(tzinfo):
    def dst(self, dt):
        return timedelta(hours=1)  # Assume DST is 1 hour

# Example usage
tz = Mytz()
print("Daylight Saving Time (DST):", tz.dst(None))  

Output
Daylight Saving Time (DST): 1:00:00

Explanation: In the example, dst(self, dt) returns timedelta(hours=1), which means the time zone has a 1-hour DST adjustment. This method is useful for handling daylight saving transitions.

3. tzname(self, dt)

tzname() method returns the name of the time zone. This is typically a string representing the time zone abbreviation (e.g., "UTC", "PST").

Python
from datetime import tzinfo

class Mytz(tzinfo):
    def tzname(self, dt):
        return "MyTimeZone"  # Custom time zone name

# Example usage
tz = Mytz()
print("Time Zone Name:", tz.tzname(None))  

Output
Time Zone Name: MyTimeZone

Explanation: In the example, tzname(self, dt) returns the string "MyTimeZone", which is a custom name for the time zone. This method helps in identifying the time zone.

4. fromutc(self, dt)

fromutc() method is used to convert a UTC time into the corresponding local time of the time zone. It adds the UTC offset to the given UTC time to return the local time.

Python
from datetime import datetime, timedelta, tzinfo

# Custom timezone class inheriting from tzinfo
class Mytz(tzinfo):
    def utcoffset(self, dt):
        return timedelta(hours=5)  

    def dst(self, dt):
        return timedelta(hours=1)  

    def tzname(self, dt):
        return "Mytz"  

    def fromutc(self, dt):
        return dt + self.utcoffset(dt)  

# Create a UTC datetime object
utc_time = datetime(2025, 4, 5, 10, 0, 0)  

# Create an object of Mytz
my_tz = Mytz()

# Convert the UTC time to local time using fromutc()
local_time = my_tz.fromutc(utc_time)

print("UTC Time:", utc_time)
print("Local Time:", local_time)

Output
UTC Time: 2025-04-05 10:00:00
Local Time: 2025-04-05 15:00:00

Explanation:

Related Articles:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4