This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: timedelta overflows in a surprising way
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: jjinux, tim.peters
Priority: normal Keywords:

Created on 2008-09-18 02:02 by jjinux, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg73353 - (view) Author: Shannon -jj Behrens (jjinux) Date: 2008-09-18 02:02
I was very surprised by the following behavior:

>>> from datetime import datetime
>>> now = datetime.today()
>>> future = datetime.today()
>>> (now - future).seconds
86395

I know that http://docs.python.org/lib/datetime-timedelta.html says
"This is exact, but may overflow", but I was really expecting a negative
number of seconds.

Feel free to close this bug if you disagree.
msg73360 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2008-09-18 04:34
Not a bug.  Try (future - now).seconds instead so that the timedelta is
positive.  As explained in the docs, a negative timedelta is normalized
so that only the .days attribute is negative, and, as the docs also say,
"normalization of negative values may be surprising at first".  The
.seconds and .microseconds attributes are always non-negative.

>>> from datetime import datetime
>>> now = datetime.today()
>>> future = datetime.today()
>>> (now - future).seconds
86390
>>> (future - now).seconds
9
>>> now - future
datetime.timedelta(-1, 86390, 146000) # only .days is negative
msg73366 - (view) Author: Shannon -jj Behrens (jjinux) Date: 2008-09-18 08:23
Yes, that makes perfect sense.  Sorry, I missed that part of the docs. 
Please feel free to close this bug.
History
Date User Action Args
2022-04-11 14:56:39adminsetgithub: 48143
2008-09-18 21:30:03benjamin.petersonsetstatus: open -> closed
resolution: not a bug
2008-09-18 08:23:18jjinuxsetmessages: + msg73366
2008-09-18 04:34:35tim.peterssetnosy: + tim.peters
messages: + msg73360
2008-09-18 02:02:34jjinuxcreate