summaryrefslogtreecommitdiff
path: root/Modules
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2010-05-09 09:30:06 +0000
committerMark Dickinson <dickinsm@gmail.com>2010-05-09 09:30:06 +0000
commit7000e9e01bb784dfc23a70a9d736613ae83c8dad (patch)
tree993dfea355ae52e05bf1d81ad01dd16790869f3b /Modules
parent860852fdf434cb566ace9879ba2a6be0e2569765 (diff)
downloadcpython-git-7000e9e01bb784dfc23a70a9d736613ae83c8dad.tar.gz
Issue #8644: Improve accuracy of timedelta.total_seconds method.
(Backport of r80979 to py3k.) Thanks Alexander Belopolsky.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/datetimemodule.c22
1 files changed, 19 insertions, 3 deletions
diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c
index fa7ef0b71b..7105c6935d 100644
--- a/Modules/datetimemodule.c
+++ b/Modules/datetimemodule.c
@@ -2096,9 +2096,25 @@ delta_getstate(PyDateTime_Delta *self)
static PyObject *
delta_total_seconds(PyObject *self)
{
- return PyFloat_FromDouble(GET_TD_MICROSECONDS(self) / 1000000.0 +
- GET_TD_SECONDS(self) +
- GET_TD_DAYS(self) * 24.0 * 3600.0);
+ PyObject *total_seconds;
+ PyObject *total_microseconds;
+ PyObject *one_million;
+
+ total_microseconds = delta_to_microseconds((PyDateTime_Delta *)self);
+ if (total_microseconds == NULL)
+ return NULL;
+
+ one_million = PyLong_FromLong(1000000L);
+ if (one_million == NULL) {
+ Py_DECREF(total_microseconds);
+ return NULL;
+ }
+
+ total_seconds = PyNumber_TrueDivide(total_microseconds, one_million);
+
+ Py_DECREF(total_microseconds);
+ Py_DECREF(one_million);
+ return total_seconds;
}
static PyObject *