]> granicus.if.org Git - python/commitdiff
Issue #11576: Fixed timedelta subtraction glitch on big timedelta values
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>
Wed, 6 Apr 2011 02:12:22 +0000 (22:12 -0400)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>
Wed, 6 Apr 2011 02:12:22 +0000 (22:12 -0400)
Lib/test/test_datetime.py
Modules/datetimemodule.c

index 1a8635307645b58b8d782dceb3578b2356d14560..d246d60797e34c9653fa65375f72abc661708dc7 100644 (file)
@@ -231,6 +231,13 @@ class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase):
         eq(a//10, td(0, 7*24*360))
         eq(a//3600000, td(0, 0, 7*24*1000))
 
+        # Issue #11576
+        eq(td(999999999, 86399, 999999) - td(999999999, 86399, 999998),
+           td(0, 0, 1))
+        eq(td(999999999, 1, 1) - td(999999999, 1, 0),
+           td(0, 0, 1))
+
+
     def test_disallowed_computations(self):
         a = timedelta(42)
 
index f907093015619f6dec2be77fe71f0b6d4769c1d0..f733fa1900f884daed8be9e2c601d55cd72a5b5b 100644 (file)
@@ -1737,13 +1737,14 @@ delta_subtract(PyObject *left, PyObject *right)
 
     if (PyDelta_Check(left) && PyDelta_Check(right)) {
         /* delta - delta */
-        PyObject *minus_right = PyNumber_Negative(right);
-        if (minus_right) {
-            result = delta_add(left, minus_right);
-            Py_DECREF(minus_right);
-        }
-        else
-            result = NULL;
+        /* The C-level additions can't overflow because of the
+         * invariant bounds.
+         */
+        int days = GET_TD_DAYS(left) - GET_TD_DAYS(right);
+        int seconds = GET_TD_SECONDS(left) - GET_TD_SECONDS(right);
+        int microseconds = GET_TD_MICROSECONDS(left) -
+                           GET_TD_MICROSECONDS(right);
+        result = new_delta(days, seconds, microseconds, 1);
     }
 
     if (result == Py_NotImplemented)