0

Why datetime and pendulum produce different results in that code snippet? What am I missing?

from pytz import timezone
from pendulum import datetime as pendulum_datetime
from datetime import datetime as datetime_datetime

tz = timezone('US/Eastern')
# both `dt_pendulum` and `dt_dt` represent the same point in time: 2022, March 25, 10am (in US/Eastern)
dt_pendulum = pendulum_datetime(2022, 3, 25, 10, 0, tz=tz)
dt_dt = datetime_datetime(2022, 3, 25, 10, 0, tzinfo=tz)

# both datetimes are further converted to `US/Pacific`
# thus, they still represent the same point in time, just in different time zone.
dt_pendulum_pacific = dt_pendulum.astimezone(tz=timezone('US/Pacific'))
dt_dt_pacific = dt_dt.astimezone(tz=timezone('US/Pacific'))

print(dt_pendulum_pacific)
print(dt_dt_pacific)

produces:

2022-03-25T07:00:00-07:00
2022-03-25 07:56:00-07:00

I'm so confused by 07:56 thing returned by datetime. I thought datetime.datetime and pendulum.datetime should produce the same result in that example.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
govordovsky
  • 359
  • 2
  • 17
  • 1
    From the `pytz` documentation: *Unfortunately using the tzinfo argument of the standard datetime constructors ‘’does not work’’ with pytz for many timezones.* It wasn't clear to me just what you're supposed to do instead. – jasonharper Mar 29 '22 at 18:39
  • @jasonharper, interesting. thanks. I found this in their doc as well: "The preferred way of dealing with times is to always work in UTC". However to work with UTC, I need to convert time to UTC, if all I have is local time in `US/Eastern`. Seems to be a chicken egg problem. – govordovsky Mar 29 '22 at 18:45
  • 1
    the TLDR: that's ***not*** how you set a time zone with pytz: `datetime_datetime(2022, 3, 25, 10, 0, tzinfo=tz)`, instead, use `tz.localize(datetime_datetime(2022, 3, 25, 10, 0))`. – FObersteiner Mar 29 '22 at 19:59
  • @FObersteiner, thanks for the pointer of the similar question. It answered my question. I'm still surprised to observe that behavior, as it feels error-prone given no warnings raised. – govordovsky Mar 29 '22 at 20:13
  • 1
    now that we have `zoneinfo` in the standard library, things should get better... but pytz is still ubiquitous - if you ever look for a [deprecations shim](https://pypi.org/project/pytz-deprecation-shim/), here it is. – FObersteiner Mar 29 '22 at 20:24
  • I see. I think that could be an answer for python >= 3.9. If you write an answer to the original question (Weird timezone issue with pytz ...) I'll upvote it. Unfortunately can't test, as I'm using python 3.8 – govordovsky Mar 29 '22 at 20:33
  • 1
    for Python 3.8, there's also [backports.zoneinfo](https://pypi.org/project/backports.zoneinfo/), or you could use [dateutil](https://dateutil.readthedocs.io/en/stable/), which follows the same semantics as `zoneinfo`. – FObersteiner Mar 29 '22 at 20:40
  • ok I've summarized my comments [here](https://stackoverflow.com/a/71668778/10197418) ;-) – FObersteiner Mar 29 '22 at 21:15
  • 1
    detailed and helpful answer, thanks, upvoted! :) – govordovsky Mar 29 '22 at 21:41

0 Answers0