Skip to content

Commit 120ed24

Browse files
committed
update delta_total_seconds
1 parent c141c4a commit 120ed24

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

Lib/_pydatetime.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,8 +827,7 @@ def plural(n):
827827

828828
def total_seconds(self):
829829
"""Total seconds in the duration."""
830-
return ((self.days * 86400 + self.seconds) * 10**6 +
831-
self.microseconds) / 10**6 + self.nanoseconds / 10**9
830+
return round(self.days * 86400 + self.seconds + self.microseconds * 1e-6 + self.nanoseconds * 1e-9, 9)
832831

833832
# Read-only field accessors
834833
@property

Modules/_datetimemodule.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3127,20 +3127,18 @@ delta_getstate(PyDateTime_Delta *self)
31273127
static PyObject *
31283128
delta_total_seconds(PyObject *op, PyObject *Py_UNUSED(dummy))
31293129
{
3130-
PyObject *total_seconds;
3131-
PyObject *total_microseconds;
3132-
total_microseconds = delta_to_microseconds(PyDelta_CAST(op));
3133-
if (total_microseconds == NULL)
3134-
return NULL;
3135-
3136-
PyObject *current_mod = NULL;
3137-
datetime_state *st = GET_CURRENT_STATE(current_mod);
3138-
3139-
total_seconds = PyNumber_TrueDivide(total_microseconds, CONST_US_PER_SECOND(st));
3140-
3141-
RELEASE_CURRENT_STATE(st, current_mod);
3142-
Py_DECREF(total_microseconds);
3143-
return total_seconds;
3130+
PyDateTime_Delta *self = PyDelta_CAST(op);
3131+
int days = GET_TD_DAYS(self);
3132+
int seconds = GET_TD_SECONDS(self);
3133+
int microseconds = GET_TD_MICROSECONDS(self);
3134+
int nanoseconds = GET_TD_NANOSECONDS(self);
3135+
double total = (double)days * 86400.0
3136+
+ (double)seconds
3137+
+ (double)microseconds * 1e-6
3138+
+ (double)nanoseconds * 1e-9;
3139+
// round to 9 decimal places
3140+
total = round(total * 1e9) / 1e9;
3141+
return PyFloat_FromDouble(total);
31443142
}
31453143

31463144
static PyObject *

0 commit comments

Comments
 (0)