1

I am having trouble understanding the effect of timezone offsets using pytz.

I start with a time in UTC, like this:

>>> utc = datetime.datetime(2021, 3, 5, 7, 59, 58, tzinfo=pytz.timezone("UTC"))

Now I take the same wall clock time in EST, which is 5 standard meridians to the west and should differ (I believe) by 5 hours.

>>> est = datetime.datetime(2021, 3, 5, 7, 59, 58, tzinfo=pytz.timezone("US/Eastern"))
>>> est-utc
datetime.timedelta(seconds=17760)
>>> 17760/3600
4.933333333333334

That difference is not the 5 hours I expect, but 4h56m, short by 4 minutes

Continuing to the west I try to arrive at times with one hour successively earlier:

>>> cst = datetime.datetime(2021, 3, 5, 7, 59, 58, tzinfo=pytz.timezone("US/Central"))
>>> cst-utc
datetime.timedelta(seconds=21060)
>>> 21060/3600
5.85

That difference is not the expected 6 hours, but 5h51, short by 9 minutes.

>>> mst = datetime.datetime(2021, 3, 5, 7, 59, 58, tzinfo=pytz.timezone("US/Mountain"))
>>> mst-utc
datetime.timedelta(seconds=25200)
>>> 25200/3600
7.0

This yields the expected 7-hour difference.

>>> pst = datetime.datetime(2021, 3, 5, 7, 59, 58, tzinfo=pytz.timezone("US/Pacific"))
>>> pst-utc
datetime.timedelta(seconds=28380)
>>> 28380/3600
7.883333333333334

Now the difference is not the expected 8 hours but 7h53, short by 7 minutes.

And if I display the times, this is what I get:

>>> est
datetime.datetime(2021, 3, 5, 7, 59, 58, tzinfo=<DstTzInfo 'US/Eastern' LMT-1 day, 19:04:00 STD>)
>>> cst
datetime.datetime(2021, 3, 5, 7, 59, 58, tzinfo=<DstTzInfo 'US/Central' LMT-1 day, 18:09:00 STD>)
>>> mst
datetime.datetime(2021, 3, 5, 7, 59, 58, tzinfo=<DstTzInfo 'US/Mountain' LMT-1 day, 17:00:00 STD>)
>>> pst
datetime.datetime(2021, 3, 5, 7, 59, 58, tzinfo=<DstTzInfo 'US/Pacific' LMT-1 day, 16:07:00 STD>)

So datetime is fully aware that the UTC offsets of these times vary from the expected even number of hours by respectively 4, 9, 0 and 7 minutes.

Clearly my expectations of how these timezone offsets should work is not correct.

I tried again with simpler timezone names: pytz recognizes EST and CST, and yields the expected whole-hour offsets for them, but rejects MST and PST.

Now, I obtained these results on Windows 10, and I can already hear an answer along the lines of "Ah, well, Windows, everybody knows that timezones on Windows are bizarre" (which is true), but they are not that bizarre, and besides, I get results identical to the ones shown above from Python 3.8 on Linux.

What am I missing here?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • see also [pytz: The Fastest Footgun in the West](https://blog.ganssle.io/articles/2018/03/pytz-fastest-footgun.html) – FObersteiner Aug 28 '23 at 18:15

1 Answers1

0

It appears that the simplest solution is not to try and understand what pytz.tzinfo is doing, but instead move to zoneinfo.ZoneInfo that was added to the standard library in Python 3.9.

BoarGules
  • 16,440
  • 2
  • 27
  • 44