sdispater / pytzdata

Official timezone database for Python.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Automatically setup zoneinfo.TZPATH

joaoe opened this issue · comments

Observations:

This affect Python 3.9+ https://docs.python.org/3.9/whatsnew/3.9.html#zoneinfo

The zoneinfo module is part of the stdlib. datetime and other 3rd party modules rely on zoneinfo to validate timezones.

e.g., in my project, we happened to use grammarie.TimeZone with pydantic.BaseSettings objects. grammarie.TimeZone just inherits from zoneinfo.ZoneInfo.

If you look at the source for zoneinfo.ZoneInfo._new_instance there is a call to obj._find_tzfile which jumps to zoneinfo._tzpath.find_tzfile , which then traverses TZPATH, which is a tuple of paths.

The default value for TZPATH on linux is setup from the value of sysconfig.get_config_var("TZPATH"), which points to the system supplied IANA time zone database. On windows it is typically empty, and thus projects must depend on the tzdata module, since the fallback zoneinfo._common.load_tzdata looks up the IANA database suplied by the tzdata module if it is installed.

Suggestion:

Well, pytzdata does not participate anywhere in this process. To do so, pytzdata must advertize itself to zoneinfo.
I had this code in my project which meets that use case.

import zoneinfo as z
z.reset_tzpath((pytzdata.tz_path("/"),) + z.TZPATH)

You can put it at the top level of the module, or preload that code with a pth file (although pth files are perhaps on their way out).
Note: yes, the call to tz_path("/") was a hack, since there is no clean API to get the root folder of the database.

To validate that pytzdata is properly setup with zoneinfo, then this should not crash

import zoneinfo as z
z.ZoneInfo("Europe/London")

Thank you.