Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pytz timezone function returns a timezone that is off by 9 minutes

For some reason which I haven't been able to figure out yet, from the the following code:

>>> from pytz import timezone >>> timezone('America/Chicago') 

I get:

<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD> 

When, I assume, I should get:

<DstTzInfo 'America/Chicago' LMT-1 day, 18:00:00 STD> 

...since I don't think that my timezone is 6 hours and 9 minutes away from UTC.

I have looked at the source code for pytz but I will admit that I haven't exactly been able to figure out what is going wrong.

I have passed other values to the timezone() function, and the values it returns appear to be correct. For some reason though, the information relevant to my timezone is not correct.

Finally, my co-worker in the cube next to me has confirmed that the function returns the correct timezone info on his machine.

Does anyone have any idea why my timezone ('America/Chicago') would be off by 9 minutes? I am running version 2015.7 of pytz installed using pip. Thank you!

like image 511
elethan Avatar asked Feb 17 '16 16:02

elethan


People also ask

Is pytz deprecated?

python3-pytz ) package will be deprecated in Fedora 35. Packages in Fedora should use Python standard library modules, such as dateutil.tz and zoneinfo (available since Python 3.9) instead. Some packages still depend on pytz , so we cannot remove it yet.

What does pytz localize do?

localize() pytz. localize() is useful for making a naive timezone aware. it is useful when a front-end client sends a datetime to the backend to be treated as a particular timezone (usually UTC).

What is pytz Python?

pytz brings the Olson tz database into Python. This library allows accurate and cross platform timezone calculations using Python 2.4 or higher. It also solves the issue of ambiguous times at the end of daylight saving time, which you can read more about in the Python Library Reference ( datetime.


2 Answers

Answer based on the answer by Carl Meyer in Google Groups Answer

The reason for this difference, is that this is NOT the right way of converting a timezone agnostic datetime object to a timezone aware object.

The explanation being:

"A pytz timezone class does not represent a single offset from UTC, it represents a geographical area which, over the course of history, has probably gone through several different UTC offsets. The oldest offset for a given zone, representing the offset from before time zones were standardized (in the late 1800s, most places) is usually called "LMT" (Local Mean Time), and it is often offset from UTC by an odd number of minutes."

(quote from the cited answer in Google Groups)

Basically, you should do:

from datetime import datetime import pytz  my_datetime = datetime(2015, 6, 11, 13, 30) my_tz = pytz.timezone('America/Chicago')     good_dt = my_tz.localize(my_datetime)  print(good_dt) 

out: 2015-06-11 13:30:00-05:00

like image 151
learn2day Avatar answered Sep 22 '22 23:09

learn2day


Unless your local timezone has a fixed UTC offset then it is pointless to talk about its specific value without providing a specific date/time.

If you provide the time e.g., the current time then you'll see that pytz produces the expected UTC offset:

>>> from datetime import datetime >>> import pytz >>> datetime.now(pytz.timezone('America/Chicago')).strftime('%Z%z') 'CST-0600' 

See

  • Datetime Timezone conversion using pytz
  • pytz localize vs datetime replace

If you don't provide a specific date/time then pytz may return an arbitrary utc offset from the set of available utc offsets for the given timezone. The recent pytz versions return utc offsets that correspond to the earliest time (LMT as a rule) but you should not rely on it. You and your friend may use different pytz versions that may explain the difference in results.

like image 36
jfs Avatar answered Sep 22 '22 23:09

jfs