tzlocal 4.1
pip install tzlocal
Latest version
Released: Oct 29, 2021
tzinfo object for the local timezone
Project description
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.
Info
This Python module returns a tzinfo object (with a pytz_deprecation_shim, for pytz compatibility) with the local timezone information, under Unix and Windows.
It requires Python 3.6 or later, and will use the backports.tzinfo package, for Python 3.6 to 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.
Supported systems
These are the systems that are in theory supported:
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.
Usage
Load the local timezone:
>>> from tzlocal import get_localzone >>> tz = get_localzone() >>> tz <DstTzInfo 'Europe/Warsaw' WMT+1:24:00 STD>
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=<DstTzInfo 'Europe/Warsaw' CEST+2:00:00 DST>)
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=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)
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.
Development
To create a development environment, create a virtualenv and make a development installation:
$ virtualenv ve $ source ve/bin/activation (Win32: .\ve\Scripts\activate) (ve) $ pip install -e .[test,devenv]
To run tests, just use pytest, coverage is nice as well:
(ve) $ pytest --cov=tzlocal
Maintainer
Lennart Regebro, regebro@gmail.com
Contributors
(Sorry if I forgot someone)
License
MIT https://opensource.org/licenses/MIT
Changes
4.1 (2021-10-29)
No changes from 4.1b1.
4.1b1 (2021-10-28)
4.0.2 (2021-10-26)
Improved the error message when you had a conflict including a /etc/localtime symlink.
4.0.1 (2021-10-19)
A long time bug in Ubuntu docker images seem to not get fixed, so I added a workaround.
4.0.1b1 (2021-10-18)
Handle UCT and Zulu as synonyms for UTC, while treating GMT and UTC as different.
4.0 (2021-10-18)
No changes.
4.0b5 (2021-10-18)
Fixed a bug in the Windows DST support.
4.0b4 (2021-10-18)
Added support for turning off DST in Windows. That only works in whole hour timezones, and honestly, if you need to turn off DST, you should just use UTC as a timezone.
4.0b3 (2021-10-08)
4.0b2 (2021-09-26)
4.0b1 (2021-08-21)
Now finds and compares all the configs (under Unix-like systems) and tells you what files it found and how they conflict. This should make it a lot easier to figure out what goes wrong.
3.0 (2021-08-13)
3.0b1 (2020-09-21)
2.1 (2020-05-08)
No changes.
2.1b1 (2020-02-08)
2.0.0 (2019-07-23)
No differences since 2.0.0b3
Major differences since 1.5.1
Minor differences and bug fixes
1.5.1 (2017-12-01)
1.5 had a bug that slipped through testing, fixed that, increased test coverage.
1.5 (2017-11-30)
1.4 (2017-04-18)
I use MIT on my other projects, so relicensing.
1.4b1 (2017-04-14)
1.3.2 (2017-04-12)
1.3 (2016-10-15)
#34: Added support for /var/db/zoneinfo
1.2.2 (2016-03-02)
#30: Fixed a bug on OS X.
1.2.1 (2016-02-28)
1.2 (2015-06-14)
Systemd stores no time zone name, forcing us to look at the name of the file that localtime symlinks to. (cameris)
1.1.2 (2014-10-18)
1.1.1 (2014-01-29)
I forgot to add Etc/UTC as an alias for Etc/GMT.
1.1 (2014-01-28)
1.0 (2013-05-29)
0.3 (2012-09-13)
0.2 (2012-09-12)
Python 3 support.
0.1 (2012-09-11)
Initial release.
Help
Installing packages
Uploading packages
User guide
FAQs
About PyPI
PyPI on Twitter
Infrastructure dashboard
Package index name retention
Our sponsors
Contributing to PyPI
Bugs and feedback
Contribute on GitHub
Translate PyPI
Development credits
Using PyPI
Code of conduct
Report security issue
Privacy policy
Terms of use
Status: All Systems Operational
Developed and maintained by the Python community, for the Python community.
Donate today!
© 2021 Python Software Foundation
Site map
Supported by
AWS
Datadog
Facebook / Instagram
Fastly
Google
Huawei
Microsoft
NVIDIA
Pingdom
Salesforce
Sentry
StatusPage
HelpSponsorsLog inRegister
Project description Project details Release history Download files