Package: tzlocal
API CHANGE!With version 3.0 of tzlocal, tzlocal no longer returnedpytzobjects, butzoneinfoobjects, which has a different API. Since 4.0, it now restored
partial compatibility forpytzusers through Paul Ganssle’spytz_deprecation_shim.tzlocal 4.0 also adds an official functionget_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, whereget_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 thepytz_deprecation_shim, and now only returnszoneinfoobjects, like verion 3.0 did. If you needpytzobjects, 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.InfoThis Python module returns theIANA time zone namefor your local time zone or atzinfoobject with the local timezone information, under Unix and Windows.It requires Python 3.8 or later, and will use thebackports.tzinfopackage, for Python 3.8.This module attempts to fix a glaring hole in thepytzandzoneinfomodules, 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.Withtzlocalyou only need to callget_localzone()and you will get atzinfoobject 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 forIt’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 Pythontimemodule.Supported systemsThese are the systems that are in theory supported:Windows 2000 and laterAny unix-like system with a/etc/localtimeor/usr/local/etc/localtimeIf 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 calledlocal, this is not
a bug, it’s actually the main feature oftzlocal, 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 usetzlocalto get the name of your local timezone, but only
if your system is configured to make that possible.tzlocallooks for the
timezone name in/etc/timezone,/var/db/zoneinfo,/etc/sysconfig/clockand/etc/conf.d/clock. If your/etc/localtimeis 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 DockerIt 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.UsageLoad 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 withzoneinfo(backports.zoneinfoon 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, useget_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, whereget_localzone()would generally succeed.TroubleshootingIf 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')DevelopmentFor 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 devenvTo run tests:$ make testCheck the syntax:$ make checkMaintainerLennart Regebro,regebro@gmail.comContributorsMarc Van OlmenBenjamen MeyerManuel EbertXiaokun ZhuCamerisEdward BettsMcK KIMCris EwingAyala ShacharLev MaximovJakub WilkJohn QuarlesPreston LandersVictor TorresJean JordaanZackary WelchMickaël SchoentgenGabriel CoronaAlex GrönholmJulin SMiroslav ŠedivýrevansSZSam TreweekPeter Di PasqualeRongrong(Sorry if I forgot someone)LicenseMIThttps://opensource.org/licenses/MIT