You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix incorrect time-zone in results after localization
Consider the following example using the canonical way to add zones to
datetime objects:
>>> import pytz
>>> import datetime
>>> import zoneinfo
>>> datetime.datetime(2023, 1, 1, tzinfo=pytz.timezone("America/Los_Angeles")).isoformat()
'2023-01-01T00:00:00-07:53'
>>> datetime.datetime(2023, 1, 1, tzinfo=zoneinfo.ZoneInfo("America/Los_Angeles")).isoformat()
'2023-01-01T00:00:00-08:00'
pytz does eager timezone evaluation and uses the local-mean-time since
the instant in time is not known. It requires an additional `localize`
call to get the correct zone like so:
>>> pytz.timezone("America/Los_Angeles").localize(datetime.datetime(2023, 1, 1)).isoformat()
'2023-01-01T00:00:00-08:00'
This increases chances of introducing bugs when writing idiomatic
Python.
The only reason to use pytz was because it allowed to control what
happens with ambiguous datetimes but the standard library also allows
provides control over that since 3.9 (and is available as
backports.zoneinfo for older versions).
0 commit comments