diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 00a9d2bca3f477..f86acf23052dc9 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -697,6 +697,29 @@ def test_total_seconds(self): td = timedelta(microseconds=ms) self.assertEqual(td.total_seconds(), td / timedelta(seconds=1)) + # Converting to double before division would lose the +1. + 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, + ): + with self.subTest(value=value): + 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..8d340fa4010c90 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -2154,15 +2154,25 @@ 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. + * The number of whole seconds always fits in int64_t, and so does the + * number of microseconds unless the delta is very large, so compute it + * in C when possible and fall back to Python int arithmetic otherwise. + * One second is reserved at the limit so that adding the microseconds + * 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 +2181,23 @@ delta_to_microseconds(PyDateTime_Delta *self) return NULL; } - x1 = PyLong_FromLong(GET_TD_DAYS(self)); + x1 = PyLong_FromInt64(seconds); /* days and seconds in 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)); /* us */ if (x2 == NULL) goto Done; - Py_SETREF(x1, NULL); - /* x2 has days in seconds */ - x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */ + /* x2 has days+seconds in us */ + Py_SETREF(x1, PyLong_FromLong(GET_TD_MICROSECONDS(self))); 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 */ - 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; }