Index: Doc/library/datetime.rst =================================================================== --- Doc/library/datetime.rst (revision 71720) +++ Doc/library/datetime.rst (working copy) @@ -188,16 +188,18 @@ Instance attributes (read-only): -+------------------+--------------------------------------------+ -| Attribute | Value | -+==================+============================================+ -| ``days`` | Between -999999999 and 999999999 inclusive | -+------------------+--------------------------------------------+ -| ``seconds`` | Between 0 and 86399 inclusive | -+------------------+--------------------------------------------+ -| ``microseconds`` | Between 0 and 999999 inclusive | -+------------------+--------------------------------------------+ - ++-------------------+--------------------------------------------+ +| Attribute | Value | ++===================+============================================+ +| ``days`` | Between -999999999 and 999999999 inclusive | ++-------------------+--------------------------------------------+ +| ``seconds`` | Between 0 and 86399 inclusive | ++-------------------+--------------------------------------------+ +| ``microseconds`` | Between 0 and 999999 inclusive | ++-------------------+--------------------------------------------+ +| ``total_seconds`` | Total seconds in the duration (includes | +| | days, seconds and microseconds) | ++-------------------+--------------------------------------------+ Supported operations: .. XXX this table is too wide! Index: Lib/test/test_datetime.py =================================================================== --- Lib/test/test_datetime.py (revision 71720) +++ Lib/test/test_datetime.py (working copy) @@ -261,6 +261,11 @@ self.assertEqual(td.seconds, seconds) self.assertEqual(td.microseconds, us) + def test_total_seconds(self): + for total_seconds in [123456.789012, -123456.789012, 0.123456, 0]: + td = timedelta(seconds=total_seconds) + self.assertEqual(td.total_seconds, total_seconds) + def test_carries(self): t1 = timedelta(days=100, weeks=-7, Index: Modules/datetimemodule.c =================================================================== --- Modules/datetimemodule.c (revision 71720) +++ Modules/datetimemodule.c (working copy) @@ -2068,6 +2068,14 @@ return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self)); } +static PyObject * +delta_total_seconds(PyObject *self, void *context) +{ + return PyFloat_FromDouble(GET_TD_MICROSECONDS(self) / 1000000.0 + + GET_TD_SECONDS(self) + + GET_TD_DAYS(self) * 24.0 * 3600.0); +} + #define OFFSET(field) offsetof(PyDateTime_Delta, field) static PyMemberDef delta_members[] = { @@ -2083,6 +2091,13 @@ {NULL} }; +static PyGetSetDef delta_getset[] = { + {"total_seconds", (getter)delta_total_seconds, NULL, + PyDoc_STR("Total number of seconds " + "(including days and microseconds).")}, + {NULL} +}; + static PyMethodDef delta_methods[] = { {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS, PyDoc_STR("__reduce__() -> (cls, state)")}, @@ -2159,7 +2174,7 @@ 0, /* tp_iternext */ delta_methods, /* tp_methods */ delta_members, /* tp_members */ - 0, /* tp_getset */ + delta_getset, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */