tzlocal


Nametzlocal JSON
Version 5.2 PyPI version JSON
download
home_page
Summarytzinfo object for the local timezone
upload_time2023-10-22 17:41:38
maintainer
docs_urlNone
author
requires_python>=3.8
licenseMIT
keywords timezone
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            tzlocal
=======

API CHANGE!
-----------

With version 3.0 of tzlocal, tzlocal no longer returned `pytz` objects, but
`zoneinfo` objects, which has a different API. Since 4.0, it now restored
partial compatibility for `pytz` users through Paul Ganssle's
`pytz_deprecation_shim`.

tzlocal 4.0 also adds an official function `get_localzone_name()` to get only
the timezone name, instead of a timezone object. On unix, it can raise an
error if you don't have a timezone name configured, where `get_localzone()`
will succeed, so only use that if you need the timezone name.

4.0 also adds way more information on what is going wrong in your
configuration when the configuration files are unclear or contradictory.

Version 5.0 removes the `pytz_deprecation_shim`, and now only returns
`zoneinfo` objects, like verion 3.0 did. If you need `pytz` objects, you have
to stay on version 4.0. If there are bugs in version 4.0, I will release
updates, but there will be no further functional changes on the 4.x branch.


Info
----

This Python module returns the `IANA time zone name
<https://www.iana.org/time-zones>`_ for your local time zone or a ``tzinfo``
object with the local timezone information, under Unix and Windows.

It requires Python 3.8 or later, and will use the ``backports.tzinfo``
package, for Python 3.8.

This module attempts to fix a glaring hole in the ``pytz`` and ``zoneinfo``
modules, that there is no way to get the local timezone information, unless
you know the zoneinfo name, and under several Linux distros that's hard or
impossible to figure out.

With ``tzlocal`` you only need to call ``get_localzone()`` and you will get a
``tzinfo`` object with the local time zone info. On some Unices you will
still not get to know what the timezone name is, but you don't need that when
you have the tzinfo file. However, if the timezone name is readily available
it will be used.

What it's not for
-----------------

It's not for converting the current time between UTC and your local time. There are
other, simpler ways of doing this. This is ig you need to know things like the name
of the time zone, or if you need to be able to convert between your time zone and
another time zone for times that are in the future or in the past.

For current time conversions to and from UTC, look in the Python ``time`` module.


Supported systems
-----------------

These are the systems that are in theory supported:

 * Windows 2000 and later

 * Any unix-like system with a ``/etc/localtime`` or ``/usr/local/etc/localtime``

If you have one of the above systems and it does not work, it's a bug.
Please report it.

Please note that if you are getting a time zone called ``local``, this is not
a bug, it's actually the main feature of ``tzlocal``, that even if your
system does NOT have a configuration file with the zoneinfo name of your time
zone, it will still work.

You can also use ``tzlocal`` to get the name of your local timezone, but only
if your system is configured to make that possible. ``tzlocal`` looks for the
timezone name in ``/etc/timezone``, ``/var/db/zoneinfo``,
``/etc/sysconfig/clock`` and ``/etc/conf.d/clock``. If your
``/etc/localtime`` is a symlink it can also extract the name from that
symlink.

If you need the name of your local time zone, then please make sure your
system is properly configured to allow that.

If your unix system doesn't have a timezone configured, tzlocal will default
to UTC.

Notes on Docker
---------------

It turns out that Docker images frequently have broken timezone setups.
This usually resuts in a warning that the configuration is wrong, or that
the timezone offset doesn't match the found timezone.

The easiest way to fix that is to set a TZ variable in your docker setup
to whatever timezone you want, which is usually the timezone your host
computer has.

Usage
-----

Load the local timezone:

    >>> from tzlocal import get_localzone
    >>> tz = get_localzone()
    >>> tz
    zoneinfo.ZoneInfo(key='Europe/Warsaw')

Create a local datetime:

    >>> from datetime import datetime
    >>> dt = datetime(2015, 4, 10, 7, 22, tzinfo=tz)
    >>> dt
    datetime.datetime(2015, 4, 10, 7, 22, tzinfo=zoneinfo.ZoneInfo(key='Europe/Warsaw'))

Lookup another timezone with ``zoneinfo`` (``backports.zoneinfo`` on Python 3.8 or earlier):

    >>> from zoneinfo import ZoneInfo
    >>> eastern = ZoneInfo('US/Eastern')

Convert the datetime:

    >>> dt.astimezone(eastern)
    datetime.datetime(2015, 4, 10, 1, 22, tzinfo=zoneinfo.ZoneInfo(key='US/Eastern'))

If you just want the name of the local timezone, use `get_localzone_name()`:

    >>> from tzlocal import get_localzone_name
    >>> get_localzone_name()
    "Europe/Warsaw"

Please note that under Unix, `get_localzone_name()` may fail if there is no zone
configured, where `get_localzone()` would generally succeed.

Troubleshooting
---------------

If you don't get the result you expect, try running it with debugging turned on.
Start a python interpreter that has tzlocal installed, and run the following code::

    import logging
    logging.basicConfig(level="DEBUG")
    import tzlocal
    tzlocal.get_localzone()

The output should look something like this, and this will tell you what
configurations were found::

    DEBUG:root:/etc/timezone found, contents:
     Europe/Warsaw

    DEBUG:root:/etc/localtime found
    DEBUG:root:2 found:
     {'/etc/timezone': 'Europe/Warsaw', '/etc/localtime is a symlink to': 'Europe/Warsaw'}
    zoneinfo.ZoneInfo(key='Europe/Warsaw')


Development
-----------

For ease of development, there is a Makefile that will help you with basic tasks,
like creating a development environment with all the necessary tools (although
you need a supported Python version installed first)::

    $ make devenv

To run tests::

    $ make test

Check the syntax::

    $ make check


Maintainer
----------

* Lennart Regebro, regebro@gmail.com

Contributors
------------

* Marc Van Olmen
* Benjamen Meyer
* Manuel Ebert
* Xiaokun Zhu
* Cameris
* Edward Betts
* McK KIM
* Cris Ewing
* Ayala Shachar
* Lev Maximov
* Jakub Wilk
* John Quarles
* Preston Landers
* Victor Torres
* Jean Jordaan
* Zackary Welch
* Mickaël Schoentgen
* Gabriel Corona
* Alex Grönholm
* Julin S
* Miroslav Šedivý
* revansSZ
* Sam Treweek
* Peter Di Pasquale
* Rongrong

(Sorry if I forgot someone)

License
-------

* MIT https://opensource.org/licenses/MIT

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "tzlocal",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "timezone",
    "author": "",
    "author_email": "Lennart Regebro <regebro@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/04/d3/c19d65ae67636fe63953b20c2e4a8ced4497ea232c43ff8d01db16de8dc0/tzlocal-5.2.tar.gz",
    "platform": null,
    "description": "tzlocal\n=======\n\nAPI CHANGE!\n-----------\n\nWith version 3.0 of tzlocal, tzlocal no longer returned `pytz` objects, but\n`zoneinfo` objects, which has a different API. Since 4.0, it now restored\npartial compatibility for `pytz` users through Paul Ganssle's\n`pytz_deprecation_shim`.\n\ntzlocal 4.0 also adds an official function `get_localzone_name()` to get only\nthe timezone name, instead of a timezone object. On unix, it can raise an\nerror if you don't have a timezone name configured, where `get_localzone()`\nwill succeed, so only use that if you need the timezone name.\n\n4.0 also adds way more information on what is going wrong in your\nconfiguration when the configuration files are unclear or contradictory.\n\nVersion 5.0 removes the `pytz_deprecation_shim`, and now only returns\n`zoneinfo` objects, like verion 3.0 did. If you need `pytz` objects, you have\nto stay on version 4.0. If there are bugs in version 4.0, I will release\nupdates, but there will be no further functional changes on the 4.x branch.\n\n\nInfo\n----\n\nThis Python module returns the `IANA time zone name\n<https://www.iana.org/time-zones>`_ for your local time zone or a ``tzinfo``\nobject with the local timezone information, under Unix and Windows.\n\nIt requires Python 3.8 or later, and will use the ``backports.tzinfo``\npackage, for Python 3.8.\n\nThis module attempts to fix a glaring hole in the ``pytz`` and ``zoneinfo``\nmodules, that there is no way to get the local timezone information, unless\nyou know the zoneinfo name, and under several Linux distros that's hard or\nimpossible to figure out.\n\nWith ``tzlocal`` you only need to call ``get_localzone()`` and you will get a\n``tzinfo`` object with the local time zone info. On some Unices you will\nstill not get to know what the timezone name is, but you don't need that when\nyou have the tzinfo file. However, if the timezone name is readily available\nit will be used.\n\nWhat it's not for\n-----------------\n\nIt's not for converting the current time between UTC and your local time. There are\nother, simpler ways of doing this. This is ig you need to know things like the name\nof the time zone, or if you need to be able to convert between your time zone and\nanother time zone for times that are in the future or in the past.\n\nFor current time conversions to and from UTC, look in the Python ``time`` module.\n\n\nSupported systems\n-----------------\n\nThese are the systems that are in theory supported:\n\n * Windows 2000 and later\n\n * Any unix-like system with a ``/etc/localtime`` or ``/usr/local/etc/localtime``\n\nIf you have one of the above systems and it does not work, it's a bug.\nPlease report it.\n\nPlease note that if you are getting a time zone called ``local``, this is not\na bug, it's actually the main feature of ``tzlocal``, that even if your\nsystem does NOT have a configuration file with the zoneinfo name of your time\nzone, it will still work.\n\nYou can also use ``tzlocal`` to get the name of your local timezone, but only\nif your system is configured to make that possible. ``tzlocal`` looks for the\ntimezone name in ``/etc/timezone``, ``/var/db/zoneinfo``,\n``/etc/sysconfig/clock`` and ``/etc/conf.d/clock``. If your\n``/etc/localtime`` is a symlink it can also extract the name from that\nsymlink.\n\nIf you need the name of your local time zone, then please make sure your\nsystem is properly configured to allow that.\n\nIf your unix system doesn't have a timezone configured, tzlocal will default\nto UTC.\n\nNotes on Docker\n---------------\n\nIt turns out that Docker images frequently have broken timezone setups.\nThis usually resuts in a warning that the configuration is wrong, or that\nthe timezone offset doesn't match the found timezone.\n\nThe easiest way to fix that is to set a TZ variable in your docker setup\nto whatever timezone you want, which is usually the timezone your host\ncomputer has.\n\nUsage\n-----\n\nLoad the local timezone:\n\n    >>> from tzlocal import get_localzone\n    >>> tz = get_localzone()\n    >>> tz\n    zoneinfo.ZoneInfo(key='Europe/Warsaw')\n\nCreate a local datetime:\n\n    >>> from datetime import datetime\n    >>> dt = datetime(2015, 4, 10, 7, 22, tzinfo=tz)\n    >>> dt\n    datetime.datetime(2015, 4, 10, 7, 22, tzinfo=zoneinfo.ZoneInfo(key='Europe/Warsaw'))\n\nLookup another timezone with ``zoneinfo`` (``backports.zoneinfo`` on Python 3.8 or earlier):\n\n    >>> from zoneinfo import ZoneInfo\n    >>> eastern = ZoneInfo('US/Eastern')\n\nConvert the datetime:\n\n    >>> dt.astimezone(eastern)\n    datetime.datetime(2015, 4, 10, 1, 22, tzinfo=zoneinfo.ZoneInfo(key='US/Eastern'))\n\nIf you just want the name of the local timezone, use `get_localzone_name()`:\n\n    >>> from tzlocal import get_localzone_name\n    >>> get_localzone_name()\n    \"Europe/Warsaw\"\n\nPlease note that under Unix, `get_localzone_name()` may fail if there is no zone\nconfigured, where `get_localzone()` would generally succeed.\n\nTroubleshooting\n---------------\n\nIf you don't get the result you expect, try running it with debugging turned on.\nStart a python interpreter that has tzlocal installed, and run the following code::\n\n    import logging\n    logging.basicConfig(level=\"DEBUG\")\n    import tzlocal\n    tzlocal.get_localzone()\n\nThe output should look something like this, and this will tell you what\nconfigurations were found::\n\n    DEBUG:root:/etc/timezone found, contents:\n     Europe/Warsaw\n\n    DEBUG:root:/etc/localtime found\n    DEBUG:root:2 found:\n     {'/etc/timezone': 'Europe/Warsaw', '/etc/localtime is a symlink to': 'Europe/Warsaw'}\n    zoneinfo.ZoneInfo(key='Europe/Warsaw')\n\n\nDevelopment\n-----------\n\nFor ease of development, there is a Makefile that will help you with basic tasks,\nlike creating a development environment with all the necessary tools (although\nyou need a supported Python version installed first)::\n\n    $ make devenv\n\nTo run tests::\n\n    $ make test\n\nCheck the syntax::\n\n    $ make check\n\n\nMaintainer\n----------\n\n* Lennart Regebro, regebro@gmail.com\n\nContributors\n------------\n\n* Marc Van Olmen\n* Benjamen Meyer\n* Manuel Ebert\n* Xiaokun Zhu\n* Cameris\n* Edward Betts\n* McK KIM\n* Cris Ewing\n* Ayala Shachar\n* Lev Maximov\n* Jakub Wilk\n* John Quarles\n* Preston Landers\n* Victor Torres\n* Jean Jordaan\n* Zackary Welch\n* Micka\u00ebl Schoentgen\n* Gabriel Corona\n* Alex Gr\u00f6nholm\n* Julin S\n* Miroslav \u0160ediv\u00fd\n* revansSZ\n* Sam Treweek\n* Peter Di Pasquale\n* Rongrong\n\n(Sorry if I forgot someone)\n\nLicense\n-------\n\n* MIT https://opensource.org/licenses/MIT\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "tzinfo object for the local timezone",
    "version": "5.2",
    "project_urls": {
        "Changelog": "https://github.com/regebro/tzlocal/blob/master/CHANGES.txt",
        "Issue tracker": "https://github.com/regebro/tzlocal/issues",
        "Source code": "https://github.com/regebro/tzlocal"
    },
    "split_keywords": [
        "timezone"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "973fc4c51c55ff8487f2e6d0e618dba917e3c3ee2caae6cf0fbb59c9b1876f2e",
                "md5": "5963f9cb3c46cab06e0e11a4437e6ca5",
                "sha256": "49816ef2fe65ea8ac19d19aa7a1ae0551c834303d5014c6d5a62e4cbda8047b8"
            },
            "downloads": -1,
            "filename": "tzlocal-5.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5963f9cb3c46cab06e0e11a4437e6ca5",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 17859,
            "upload_time": "2023-10-22T17:41:36",
            "upload_time_iso_8601": "2023-10-22T17:41:36.511688Z",
            "url": "https://files.pythonhosted.org/packages/97/3f/c4c51c55ff8487f2e6d0e618dba917e3c3ee2caae6cf0fbb59c9b1876f2e/tzlocal-5.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "04d3c19d65ae67636fe63953b20c2e4a8ced4497ea232c43ff8d01db16de8dc0",
                "md5": "ccd7e41048de71f2b49e702582f43f94",
                "sha256": "8d399205578f1a9342816409cc1e46a93ebd5755e39ea2d85334bea911bf0e6e"
            },
            "downloads": -1,
            "filename": "tzlocal-5.2.tar.gz",
            "has_sig": false,
            "md5_digest": "ccd7e41048de71f2b49e702582f43f94",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 30201,
            "upload_time": "2023-10-22T17:41:38",
            "upload_time_iso_8601": "2023-10-22T17:41:38.449958Z",
            "url": "https://files.pythonhosted.org/packages/04/d3/c19d65ae67636fe63953b20c2e4a8ced4497ea232c43ff8d01db16de8dc0/tzlocal-5.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-22 17:41:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "regebro",
    "github_project": "tzlocal",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "tzlocal"
}
        
Elapsed time: 0.36912s