|
| 1 | +tzdata: Python package providing IANA time zone data |
| 2 | +==================================================== |
| 3 | + |
| 4 | +This is a Python package containing ``zic``-compiled binaries for the IANA time |
| 5 | +zone database. It is intended to be a fallback for systems that do not have |
| 6 | +system time zone data installed (or don't have it installed in a standard |
| 7 | +location), as a part of `PEP 615 <https://www.python.org/dev/peps/pep-0615/>`_ |
| 8 | + |
| 9 | +This repository generates a ``pip``-installable package, published on PyPI as |
| 10 | +`tzdata <https://pypi.org/project/tzdata>`_. |
| 11 | + |
| 12 | +Overview |
| 13 | +-------- |
| 14 | + |
| 15 | +``tzdata`` is a *data-only* package, organized in such a way that its resources |
| 16 | +are accessible via :mod:`importlib.resources` (or, in older versions of Python, |
| 17 | +its backport `importlib_resources`_). Although ``importlib.resources`` or |
| 18 | +equivalent is recommended, it is also possible to access the data via |
| 19 | +:func:`pkgutil.get_data` as well. |
| 20 | + |
| 21 | +.. TODO: Change ``zoneinfo`` to :mod:`zoneinfo` when 3.9 is released |
| 22 | +
|
| 23 | +It is primarily intended to be used by standard library's ``zoneinfo`` |
| 24 | +module (new in Python 3.9), but it is also available as a source for time zone |
| 25 | +data for other time zone libraries. It is generally recommended that any time |
| 26 | +zone libraries should attempt to use the system data before using the |
| 27 | +``tzdata`` package, but some systems (notably Windows) do not deploy zoneinfo |
| 28 | +binaries of this type, and so ``tzdata`` is necessary. |
| 29 | + |
| 30 | +Contents |
| 31 | +-------- |
| 32 | + |
| 33 | +The ``tzdata`` package provides the output of ``zic`` compilation (the |
| 34 | +equivalent of something like ``/usr/share/zoneinfo``) under the |
| 35 | +``tzdata.zoneinfo`` package unaltered. This includes the ``tzdata.zi`` file, |
| 36 | +which is a compact text representation of the un-compiled time zone database. |
| 37 | + |
| 38 | +The package organization looks something like this:: |
| 39 | + |
| 40 | + src/tzdata |
| 41 | + ├── __init__.py |
| 42 | + ├── zoneinfo |
| 43 | + │ ├── __init__.py |
| 44 | + │ ├── Africa |
| 45 | + │ │ ├── __init__.py |
| 46 | + │ │ ├── Abidjan |
| 47 | + │ │ ├── Accra |
| 48 | + │ │ ├── … |
| 49 | + │ │ └── Windhoek |
| 50 | + │ ├── America |
| 51 | + │ │ ├── __init__.py |
| 52 | + │ │ ├── Adak |
| 53 | + │ │ ├── Anchorage |
| 54 | + │ │ ├── … |
| 55 | + │ │ └── Yellowknife |
| 56 | + │ ├── … |
| 57 | + │ ├── tzdata.zi |
| 58 | + │ ├── … |
| 59 | + │ ├── zone.tab |
| 60 | + │ └── Zulu |
| 61 | + └── zones |
| 62 | + |
| 63 | + 21 directories, 623 files |
| 64 | + |
| 65 | +In addition to the zoneinfo files, it also provides a small amount of extra |
| 66 | +metadata about the time zones. The ``tzdata.zones`` file is a newline-delimited |
| 67 | +file listing all the IANA keys for time zone files present in the |
| 68 | +``tzdata.zoneinfo`` package. The version of the IANA data is provided as a |
| 69 | +Python variable as ``tzdata.IANA_VERSION``. |
| 70 | + |
| 71 | +Examples |
| 72 | +-------- |
| 73 | + |
| 74 | +End users should generally **not** need to use this package directly and should |
| 75 | +use a Python library like :mod:`dateutil.tz` or `zoneinfo`_, like so: |
| 76 | + |
| 77 | +.. code-block:: python |
| 78 | +
|
| 79 | + # Python 3.9+ |
| 80 | + from datetime import datetime |
| 81 | + from zoneinfo import ZoneInfo |
| 82 | +
|
| 83 | + dt = datetime.now(ZoneInfo("America/New_York")) |
| 84 | +
|
| 85 | +For those writing time zone libraries, the recommended mechanism for access is |
| 86 | +to open the relevant zoneinfo binaries with |
| 87 | +:func:`importlib.resources.open_binary`, like so: |
| 88 | + |
| 89 | +.. code-block:: python |
| 90 | +
|
| 91 | + from importlib import resources |
| 92 | +
|
| 93 | + # America/New_York |
| 94 | + with resources.open_binary("tzdata.zoneinfo.America", "New_York") as f: |
| 95 | + assert f.read(4) == b"TZif" |
| 96 | +
|
| 97 | +Note that the way this is organized, each folder in ``tzdata.zoneinfo`` is a |
| 98 | +package with resources below it. An example function for converting IANA keys |
| 99 | +to package names: |
| 100 | + |
| 101 | +.. code-block:: python |
| 102 | +
|
| 103 | + def iana_key_to_resource(key): |
| 104 | + package_loc, resource = key.rsplit("/", 1) |
| 105 | + package = "tzdata.zoneinfo." + package_loc.replace("/", ".") |
| 106 | +
|
| 107 | + return package, resource |
| 108 | +
|
| 109 | + assert iana_key_to_resource("America/New_York") == \ |
| 110 | + ("tzdata.zoneinfo.America", "New_York") |
| 111 | + assert iana_key_to_resource("America/Indiana/Indianapolis") == \ |
| 112 | + ("tzdata.zoneinfo.America.Indiana", "Indianapolis") |
| 113 | +
|
| 114 | +.. Links |
| 115 | +
|
| 116 | +.. _importlib_resources: https://importlib-resources.readthedocs.io/en/latest/ |
| 117 | +.. _zoneinfo: https://docs.python.org/3/library/zoneinfo.html |
| 118 | + |
| 119 | +.. toctree:: |
| 120 | + :maxdepth: 2 |
| 121 | + :caption: Contents: |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +Indices and tables |
| 126 | +================== |
| 127 | + |
| 128 | +* :ref:`genindex` |
| 129 | +* :ref:`modindex` |
| 130 | +* :ref:`search` |
0 commit comments