1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
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
|