-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
datetime calculations with zoneinfo don't work #91618
Comments
@jribbens This is working as intended. Arithmetic in datetimes is under-defined, and There's not really anything to be done about it, though in the future I think it would be nice for us to add some methods for explicitly doing "absolute time" calculations. I'm going to close this as the behavior is working as intended and there's nothing we can change about it. Thank you for taking the time to report your issue — this was a very clear and well-written bug report. I do wish there were a better way to resolve this. |
@pganssle Thanks. I read your blog-post before filing the bug report (which is partly why I chose the examples I did, which cannot be explained by claiming it's implementing "wall time"). I think your blog is incorrect in describing Python's datetime addition/subtraction as "wall time" - I don't think that was the intent with which it was programmed, nor is it what it outputs. I'd just describe what it does as "wrong". As per my original note above, I appreciate that this can't necessarily be fixed in the code, but I think it is a mistake to close this bug report as the documentation certainly can and in my opinion should be improved. You might say that this is a very long-standing issue (dating back to Python 2.3 when So I reiterate my suggestion that the documentation should be improved. The |
I think you may have failed to understand what my blog post is saying, and what the documentation says. It is definitely not "wrong", it's just not the definition you expect. The value returned by subtraction between two
Note that this is the case where "both are aware and have the same If I had time, I'd certainly write a more thorough guide to I'd be happy to accept a PR to the |
@pganssle I am certainly not understanding how on earth you could say the examples I gave in this bug report are not straight-up wrong. The result of the first calculation is a datetime that doesn't exist. There is no sane definition that can be applied that makes that not wrong. And even your own explanation of subtraction "The value returned by subtraction between two datetime objects in the same zone doesn't have anything to do with the amount of time that has elapsed between the two datetimes" is simply another way of saying it's wrong. This has nothing to do with "my desired semantics". I am afraid I cannot suggest documentation to fix this problem if your view is that the results given are "the right answer" to a secret undocumented question that is presumably a non-Euclidian horror that mortal man is not meant to wot of. I find it astonishing that it is viewed as acceptable that the documentation is extremely misleading and explicitly advises code that will produce bizarre results nobody expects and will inevitably cause many bugs in Python programs for years to come. |
I'm sorry that you feel that way. This state of affairs is actually deliberate and represents a design trade-off. Probably it's not the choice I'd make today for many reasons, but it's not an invalid choice. The people who originally designed these modules are quite smart and so I tend to have quite a bit of humility when I see what strikes me as a bizarre choice, and I often find that this pays dividends. There's approximately no chance that this will change, as it would be a major breaking change to do so. Rest assured that I'm aware of the fact that people find it counter-intuitive, which is why I'm constantly writing articles and giving talks on this subject. If you're at PyCon this year feel free to track me down and maybe I can explain it better in person. The perspective of someone who has changed their mind is always extremely valuable, since they know what about the explanation made things "click". Thank you again for your feedback. |
@pganssle Ok just a final comment since you still seem to be missing my point: I'm saying it's the documentation that needs changing, not the code (and mostly I'm saying that the new documentation for |
Consider the following (Europe had DST change last night, the night of 25 March/26 March). >>> dt1 = datetime(2023, 3, 25, 12, tzinfo=ZoneInfo("Europe/Paris"))
>>> dt2 = datetime(2023, 3, 26, 12, tzinfo=ZoneInfo("Europe/Paris"))
>>> dt3 = datetime(2023, 3, 26, 12, tzinfo=ZoneInfo("Europe/Berlin"))
>>> str(dt2 - dt1) # same ZoneInfo, so naive
'1 day, 0:00:00'
>>> str(dt3 - dt1) # diff ZoneInfo, but same offsets
'23:00:00' Europe/Paris and Europe/Berlin are the same timezone (CET/CEST) in spring 2023. But somehow only when the key string to ZoneInfo is different this calculation succeeds (to the intuition of some, including myself). A footnote (according to my intuition) may not be enough. I think the docs should have a warning box to this effect:
That would seem to be the preferred logic, as the docs have warning boxes for all methods that have Including a recipe for absolute addition and absolute substraction in the box would probably be of great help. |
Bug report
Doing calculations with
datetime
objects withzoneinfo
timezones doesn't work - they completely ignore the timezone. For example:2022-03-27 01:30 is a time that doesn't exist in the Europe/London timezone.
Similarly:
The difference between
d1
andd0
is 2 hours, not 3 hours.I guess this might not be fixable, in which case at the very least the
datetime
module documentation needs to be updated to say that you simply cannot do arithmetic withdatetime
objects unless they are timezone-naive or using the UTC timezone.Your environment
The text was updated successfully, but these errors were encountered: