zoneinfo module in python - IANA time zone

0
pthon zoneinfo module

zoneinfo is included in python standard modules and is very helpful while working with timezones. zoneinfo provides the complete support of the IANA time zone. The time zone that is used by the zoneinfo is basically the IANA time zone database.

What is the IANA time zone?

The word IANA stands for Internet Assigned Numbers Authority. IANA is an organization that is looking into the DNS Root, IP Addressing, and other such things as the international standard for the time zones.

The main things that the IANA is looking for are Root Zone Management, Protocol Registries and so many others.

Besides all these IANA have a list of databases for the Time zone this is called the IANA time Zone Databases.

How to use the IANA time Zone with Python?

Python provides us with the zoneinfo module, which is built in the python module that we need to install, which can help us to have access to the IANA TimeZone Databases. The IANA Time Zone Databases contains code and data which represents the history of the local time. The Time Zone database is updated timely.

We can use the python zoneinfo module which helps us get access to these Time zone databases.

How zoneinfo module works?

The python zoneinfo module uses the system's time zone if it is available, if there is no timezone data available in the system then it will use the tzdata to get its values.

class ZoneInfo(tzinfo):
    _strong_cache_size = 8
    _strong_cache = collections.OrderedDict()
    _weak_cache = weakref.WeakValueDictionary()
    __module__ = "zoneinfo"

If you are creating a project for cross-platform, then you need to declare the dependency on tzdata.

Handling daylight saving with Python zoneinfo

The good thing about using the zoneinfo is it is compatible with datetimbe arithmetics and we can easily do calculations on it. for example look at the following code how we add 1 day to the date and it changed the month as well. The month changed due to the compatibility of the arithmetics of the datetime due to the zoneinfo.

from zoneinfo import ZoneInfo
from datetime import datetime, timedelta

# getting the datetime object with zoneinfo 
dt = datetime(2020, 10, 31, 12, tzinfo=ZoneInfo("America/Los_Angeles"))
print(dt)
# adding one day to the timezone
dt_add = dt + timedelta(days=1)
print(dt_add)

Just make sure you are running the above program on a python version above 3.7. If you are not doing so it will give you an error.

Find all the available time zones with zoneinfo

you can call the function available_timezones() to get all the timezones that are valid keys for IANA time zones anywhere on the time zone path. This is time expensive as it is not getting cashed instead it is recalculated on every call to the function.

Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !