diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 00a9d2bca3f477..ac4f7adf70d0aa 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -697,6 +697,28 @@ def test_total_seconds(self): td = timedelta(microseconds=ms) self.assertEqual(td.total_seconds(), td / timedelta(seconds=1)) + # Converting the numerator to double before division loses a microsecond. + us = 2**53 + 1 + td = timedelta(microseconds=us) + self.assertEqual(td.total_seconds(), us / 1_000_000) + + def test_to_microseconds_int64_boundary(self): + us_per_day = 86_400 * 1_000_000 + max_seconds = (2**63 - 1) // 1_000_000 - 1 + first_fast = -max_seconds * 1_000_000 + last_fast = max_seconds * 1_000_000 + 999_999 + + for value in ( + timedelta.min.days * us_per_day, + -2**63 - 1, -2**63, + first_fast - 1, first_fast, + last_fast, last_fast + 1, + 2**63 - 1, 2**63, + (timedelta.max.days + 1) * us_per_day - 1, + ): + td = timedelta(microseconds=value) + self.assertEqual(td // timedelta.resolution, value) + def test_carries(self): t1 = timedelta(days=100, weeks=-7, diff --git a/Misc/NEWS.d/next/Library/2026-11-23-02-10-10.gh-issue-162467.6hMRI-.rst b/Misc/NEWS.d/next/Library/2026-11-23-02-10-10.gh-issue-162467.6hMRI-.rst new file mode 100644 index 00000000000000..fccff39902eb5e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-23-02-10-10.gh-issue-162467.6hMRI-.rst @@ -0,0 +1,3 @@ +Speed up :meth:`datetime.timedelta.total_seconds` and +:meth:`datetime.datetime.timestamp` on aware datetimes. Patch by Maurycy +Pawłowski-Wieroński. diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 091da059afd34b..796d6c57adef37 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -2154,15 +2154,22 @@ diff_to_bool(int diff, int op) /* Convert a timedelta to a number of us, * (24*3600*self.days + self.seconds)*1000000 + self.microseconds * as a Python int. - * Doing mixed-radix arithmetic by hand instead is excruciating in C, - * due to ubiquitous overflow possibilities. + * Whole seconds always fit in int64_t. Reserve one second at the limit + * so adding the microsecond field cannot overflow. */ static PyObject * delta_to_microseconds(PyDateTime_Delta *self) { + const int64_t max_seconds = INT64_MAX / 1000000 - 1; + int64_t seconds = (int64_t)GET_TD_DAYS(self) * (24 * 3600) + + GET_TD_SECONDS(self); + if (-max_seconds <= seconds && seconds <= max_seconds) { + return PyLong_FromInt64(seconds * 1000000 + + GET_TD_MICROSECONDS(self)); + } + PyObject *x1 = NULL; PyObject *x2 = NULL; - PyObject *x3 = NULL; PyObject *result = NULL; PyObject *current_mod; @@ -2171,42 +2178,21 @@ delta_to_microseconds(PyDateTime_Delta *self) return NULL; } - x1 = PyLong_FromLong(GET_TD_DAYS(self)); + x1 = PyLong_FromInt64(seconds); if (x1 == NULL) goto Done; - x2 = PyNumber_Multiply(x1, CONST_SEC_PER_DAY(st)); /* days in seconds */ + x2 = PyNumber_Multiply(x1, CONST_US_PER_SECOND(st)); if (x2 == NULL) goto Done; - Py_SETREF(x1, NULL); - - /* x2 has days in seconds */ - x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */ - if (x1 == NULL) - goto Done; - x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */ - if (x3 == NULL) - goto Done; - Py_DECREF(x1); - Py_DECREF(x2); - /* x1 = */ x2 = NULL; - - /* x3 has days+seconds in seconds */ - x1 = PyNumber_Multiply(x3, CONST_US_PER_SECOND(st)); /* us */ + Py_SETREF(x1, PyLong_FromLong(GET_TD_MICROSECONDS(self))); if (x1 == NULL) goto Done; - Py_SETREF(x3, NULL); - - /* x1 has days+seconds in us */ - x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self)); - if (x2 == NULL) - goto Done; - result = PyNumber_Add(x1, x2); + result = PyNumber_Add(x2, x1); assert(result == NULL || PyLong_CheckExact(result)); Done: Py_XDECREF(x1); Py_XDECREF(x2); - Py_XDECREF(x3); RELEASE_CURRENT_STATE(st, current_mod); return result; }