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
Documentation for datetime substract operation incorrect? #74701
Comments
In the documentation for the supported arithmetic operations for a datetime object, there is the following note, among other: datetime2 = datetime1 - timedelta Comment: While reading the source code for __sub__ operation I found in the first few lines: Line 1885: Is the documentation in contradiction with the actual implementation? |
The C "_datetime" implementation seems to handle this as documented. But either way, the "timedelta" range is greater than the "datetime" range, so it seems to be just a difference in OverflowError messages, not a big practical problem: Python "datetime" implementation:
>>> import sys
>>> sys.modules["_datetime"] = None
>>> from datetime import *
>>> datetime.max - timedelta.max
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Martin\AppData\Local\Programs\Python\Python35-32\lib\datetime.py", line 1741, in __sub__
return self + -other
File "C:\Users\Martin\AppData\Local\Programs\Python\Python35-32\lib\datetime.py", line 518, in __neg__
-self._microseconds)
File "C:\Users\Martin\AppData\Local\Programs\Python\Python35-32\lib\datetime.py", line 430, in __new__
raise OverflowError("timedelta # of days is too large: %d" % d)
OverflowError: timedelta # of days is too large: -1000000000
C "_datetime" implementation:
>>> from datetime import *
>>> datetime.max - timedelta.max
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: date value out of range |
I agree. The documentation can be improved here. The note about x - y not being quite the same as x + (-y) belongs to the timedelta - timedelta operation. It should be removed from both date - timedelta and datetime-timedelta footnotes. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: