From 04d7605912058a4f783ff9992c021cfb5f432fb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Mon, 23 Nov 2026 01:54:35 +0100 Subject: [PATCH 1/7] up --- Modules/_datetimemodule.c | 42 +++++++++++++-------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) 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; } From 1a4dca2c9e82a735476c8f5a73b7d24a25c70853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Mon, 23 Nov 2026 01:54:42 +0100 Subject: [PATCH 2/7] test --- Lib/test/datetimetester.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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, From 7cda638004059df061c8008b3862e75dc1564d07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Mon, 23 Nov 2026 02:10:15 +0100 Subject: [PATCH 3/7] news --- .../Library/2026-11-23-02-10-10.gh-issue-162467.6hMRI-.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-23-02-10-10.gh-issue-162467.6hMRI-.rst 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. From 5eb9e0477f2238963b99b5a638cab155ad05cecf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Mon, 23 Nov 2026 07:26:05 +0100 Subject: [PATCH 4/7] bring back the comments --- Modules/_datetimemodule.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 796d6c57adef37..f844ef74882e0e 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -2178,12 +2178,14 @@ delta_to_microseconds(PyDateTime_Delta *self) return NULL; } - x1 = PyLong_FromInt64(seconds); + x1 = PyLong_FromInt64(seconds); /* days and seconds in seconds */ if (x1 == NULL) goto Done; - x2 = PyNumber_Multiply(x1, CONST_US_PER_SECOND(st)); + x2 = PyNumber_Multiply(x1, CONST_US_PER_SECOND(st)); /* us */ if (x2 == NULL) goto Done; + + /* x2 has days+seconds in us */ Py_SETREF(x1, PyLong_FromLong(GET_TD_MICROSECONDS(self))); if (x1 == NULL) goto Done; From 0391e593c8c58cd056b36f839d7a0b97b95b75e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Mon, 23 Nov 2026 21:37:19 +0100 Subject: [PATCH 5/7] Update Modules/_datetimemodule.c Co-authored-by: Stan Ulbrych --- Modules/_datetimemodule.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index f844ef74882e0e..8d340fa4010c90 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -2154,8 +2154,11 @@ 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. - * Whole seconds always fit in int64_t. Reserve one second at the limit - * so adding the microsecond field cannot overflow. + * 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) From 333c6b095b00002c5df1ad9e16e47438f1fa14fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Mon, 23 Nov 2026 21:39:28 +0100 Subject: [PATCH 6/7] use subTest --- Lib/test/datetimetester.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index ac4f7adf70d0aa..5dac5a14222c76 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -716,8 +716,9 @@ def test_to_microseconds_int64_boundary(self): 2**63 - 1, 2**63, (timedelta.max.days + 1) * us_per_day - 1, ): - td = timedelta(microseconds=value) - self.assertEqual(td // timedelta.resolution, value) + with self.subTest(value=value): + td = timedelta(microseconds=value) + self.assertEqual(td // timedelta.resolution, value) def test_carries(self): t1 = timedelta(days=100, From 31faa0d094d5ff589e85f09fc2ec41295474feb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Tue, 24 Nov 2026 09:50:37 +0100 Subject: [PATCH 7/7] reword --- Lib/test/datetimetester.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 5dac5a14222c76..f86acf23052dc9 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -697,7 +697,7 @@ 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. + # 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)