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
{{ message }}
Repository navigation
issue from subtracting two datetime objects using the same timezone object, but the timezone object varies across Daylight Saving Time #105254
The answer points out the issue:
"This seems to be unintended behavior due to the implementation of the sub() method of datetime (link to github). When you subtract two datetime objects, it checks if the time zones are the same object.
if self._tzinfo is other._tzinfo:
return base
Since the tz objects passed into t1 and t2 from dateutil is the same object, the datetime object assumes the timezone stays the same and does not take into account daylight savings.
This seems to be a bug and you should definitely open a github issue about it."
My question on stackoverflow is below:
I am learning the timezone and Daylight Saving Time in python. When I calculate the time difference around Daylight Saving time using two methods, they are supposed to give the same time difference, however, they give different results.
As you can see t1 is equal to spring_ahead_159am, and t2 is equal to spring_ahead_3am. So how come (t2 - t1 ) != (spring_ahead_3am - spring_ahead_159am)?
Activity
gaogaotiantian commentedon Jun 3, 2023
This seems like a bug in
dateutilrather than CPython. The timezone info it returns(eastern) has an incorrect implementation forutcoffset.These two should not be equal because
dstis set int2.tzinfo(oreasternin this case).FObersteiner commentedon Jun 4, 2023
I think this is a case of expected elapsed duration vs. what you get from Python's timedelta arithmetic is time elapsed on a wall clock. @pganssle has a blog post on this, Semantics of timezone-aware datetime arithmetic.
EX:
--> not a bug, it's a feature.
pganssle commentedon Jun 4, 2023
Yes, this is not unintended behavior. For better or for worse, as @FObersteiner suggests, this is deliberate.