[Datetime-SIG] Zoneinfo parser in Python 3.8

Paul G paul at ganssle.io
Fri Jan 19 12:53:07 EST 2018


So I've been thinking over the idea of shipping the IANA zones as part of a Python "batteries included" type approach - which is something I've been negative about in the past because I think that it's really not a good idea to tie the zoneinfo data releases to any sort of binary or library release (in fact, I'm planning on moving dateutil's zoneinfo tarball into a separate package as of python-dateutil>=2.8.0, so that they can be updated out of cycle from one another).

That said, it's still somewhat glaring in its absence. Thinking about it, though, I think it's not at all unreasonable to include a zoneinfo *parser* into Python's `datetime` library, something similar to `dateutil.tz.tzfile`, where you can give it a zic-compiled binary and it will create a zoneinfo file from that. Additionally, `datetime` can do what `pytz` and `dateutil` *already* do, which is to use the system `zoneinfo` files *if they exist*. This would allow third party libraries shipping *only* the `tzdata` to supply the tzdata on platforms that down't ship with their own copies of the database.

Here's a rough sketch of the interface I'm thinking about (specific names don't matter, just the general concept):

1. datetime.TZPATH - tzdata search path (configurable at build time, defaults to `['/usr/share/zoneinfo']`) similar to `sys.path`
2. PYTHONTZPATH - environment variable similar to PYTHONPATH - this is prepended to the search path when importing `datetime`
3. datetime.tzfile(tzname, tzpath=None)
  -  Searches the `tzpath` for `tzname`, if `tzpath` is not None, it should be list of locations to search for `tzname` in.
4. datetime.tzfile.from_stream(tzstream, name=None)
  - Create a tzfile from an arbitrary stream `tzstream`, with optional zone name `name` (this allows you to read something from a tarball for example)

For Windows and other platforms that do *not* ship with zoneinfo, third party libraries could provide the zoneinfo data either by manipulating `datetime.TZPATH` or by wrapping either `datetime.tzfile` or `datetime.tzfile.from_stream`.

I'd be happy to put together a PEP on this if people like the idea.

Best,
Paul

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/datetime-sig/attachments/20180119/676b3999/attachment.sig>


More information about the Datetime-SIG mailing list