Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
42 changes: 14 additions & 28 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
Loading