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: datetime.datetime subtraction bug
Type: Stage:
Components: Extension Modules Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: dfugate_ms, tim.peters
Priority: normal Keywords:

Created on 2006-10-02 23:16 by dfugate_ms, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg30137 - (view) Author: David Fugate (dfugate_ms) Date: 2006-10-02 23:16
Python version: 2.5
OS name and version: Windows XP SP2
Email: dfugate@microsoft.com

The datetime.datetime subtraction operator seems to
have a bug in it:

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC
v.1310 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for
more information.
>>> import datetime
>>> x = datetime.datetime(2006, 9, 29, 15, 37, 28, 686001)
>>> y = datetime.datetime(2006, 9, 29, 15, 37, 28, 686000)
>>> x - y
datetime.timedelta(0, 0, 1)
>>> y - x
datetime.timedelta(-1, 86399, 999999)

Here, the result of y - x should be timedelta(0, 0, -1).
msg30138 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2006-10-02 23:27
Logged In: YES 
user_id=31435

Sorry, nope.  As the timedelta docs say:

"""
...

days, seconds and microseconds are then normalized so that
the representation is unique, with 

0 <= microseconds < 1000000 
0 <= seconds < 3600*24 (the number of seconds in one day) 
-999999999 <= days <= 999999999 

...

If the normalized value of days lies outside the indicated
range, OverflowError is raised. 

Note that normalization of negative values may be surprising
at first. For example, 

>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
"""

Which is exactly what you're seeing.
History
Date User Action Args
2022-04-11 14:56:20adminsetgithub: 44071
2006-10-02 23:16:35dfugate_mscreate