]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merged revisions 76529 via svnmerge from
authorAntoine Pitrou <solipsis@pitrou.net>
Wed, 25 Nov 2009 23:02:32 +0000 (23:02 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Wed, 25 Nov 2009 23:02:32 +0000 (23:02 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r76529 | antoine.pitrou | 2009-11-25 23:59:36 +0100 (mer., 25 nov. 2009) | 4 lines

  Issue #5788: `datetime.timedelta` objects get a new `total_seconds()` method returning
  the total number of seconds in the duration.  Patch by Brian Quinlan.
........

Doc/library/datetime.rst
Lib/test/test_datetime.py
Misc/NEWS
Modules/datetimemodule.c

index 68ae586037253cba2ca289e37085a811e077a33a..5126821d551a9ae2d83973ee0f4dd4592ee76bd7 100644 (file)
@@ -267,12 +267,24 @@ comparison is ``==`` or ``!=``.  The latter cases return :const:`False` or
 efficient pickling, and in Boolean contexts, a :class:`timedelta` object is
 considered to be true if and only if it isn't equal to ``timedelta(0)``.
 
+Instance methods:
+
+.. method:: timedelta.total_seconds()
+
+   Return the total number of seconds contained in the duration. Equivalent to
+   ``td.microseconds / 1000000 + td.seconds + td.days * 24 * 3600``.
+
+   .. versionadded:: 3.2
+
+
 Example usage:
 
     >>> from datetime import timedelta
     >>> year = timedelta(days=365)
     >>> another_year = timedelta(weeks=40, days=84, hours=23,
     ...                          minutes=50, seconds=600)  # adds up to 365 days
+    >>> year.total_seconds()
+    31536000.0
     >>> year == another_year
     True
     >>> ten_years = 10 * year
index 8bf8420929c94dab84bb523f116bf3fed13c9222..d85217ddc9b0a912a3137376223c72aeb010bb04 100644 (file)
@@ -261,6 +261,13 @@ class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase):
         self.assertEqual(td.seconds, seconds)
         self.assertEqual(td.microseconds, us)
 
+    def test_total_seconds(self):
+        td = timedelta(days=365)
+        self.assertEqual(td.total_seconds(), 31536000.0)
+        for total_seconds in [123456.789012, -123456.789012, 0.123456, 0, 1e6]:
+            td = timedelta(seconds=total_seconds)
+            self.assertEqual(td.total_seconds(), total_seconds)
+
     def test_carries(self):
         t1 = timedelta(days=100,
                        weeks=-7,
index 70e0e1cd54dea6191d009178e4afd533d02c8b26..c7df776f9015f1e2353e293033c5d08365fd5d72 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -140,6 +140,10 @@ C-API
 Library
 -------
 
+- Issue #5788: `datetime.timedelta` objects get a new `total_seconds()`
+  method returning the total number of seconds in the duration.  Patch by
+  Brian Quinlan.
+
 - Issue #7133: SSL objects now support the new buffer API.
 
 - Issue #1488943: difflib.Differ() doesn't always add hints for tab characters
index 8ba34742751c96490e51941e7b804aba32c673b3..764de34c367dc6467d9e60420fa812aaf86b0618 100644 (file)
@@ -2062,6 +2062,14 @@ delta_getstate(PyDateTime_Delta *self)
                                    GET_TD_MICROSECONDS(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);
+}
+
 static PyObject *
 delta_reduce(PyDateTime_Delta* self)
 {
@@ -2084,7 +2092,10 @@ static PyMemberDef delta_members[] = {
 };
 
 static PyMethodDef delta_methods[] = {
-       {"__reduce__", (PyCFunction)delta_reduce,     METH_NOARGS,
+       {"total_seconds", (PyCFunction)delta_total_seconds, METH_NOARGS,
+        PyDoc_STR("Total seconds in the duration.")},
+
+       {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS,
         PyDoc_STR("__reduce__() -> (cls, state)")},
 
        {NULL,  NULL},