Chris AtLee added the comment:

I keep needing to know the number of seconds that a timedelta represents,
so I implemented the following patch.

This returns only the integer portion, but could be modified to return a
floating point value.

----------
nosy: +catlee
Added file: http://bugs.python.org/file8837/timedelta.diff

_____________________________________
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1673409>
_____________________________________
Index: Doc/library/datetime.rst
===================================================================
--- Doc/library/datetime.rst	(revision 59236)
+++ Doc/library/datetime.rst	(working copy)
@@ -266,7 +266,13 @@
 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.tosecs()
+
+   Returns the total number of seconds this timedelta object represents.
+   This is equivalent to (timedelta.days * 3600 * 24) + timedelta.seconds
+
 .. _datetime-date:
 
 :class:`date` Objects
Index: Lib/test/test_datetime.py
===================================================================
--- Lib/test/test_datetime.py	(revision 59236)
+++ Lib/test/test_datetime.py	(working copy)
@@ -475,6 +475,11 @@
         self.assertEqual(str(t3), str(t4))
         self.assertEqual(t4.as_hours(), -1)
 
+    def test_tosecs(self):
+        for s in (100, 1000, 10000000, 100000000000):
+            td = timedelta(seconds = s)
+            self.assertEqual(td.tosecs(), s)
+
 #############################################################################
 # date tests
 
Index: Modules/datetimemodule.c
===================================================================
--- Modules/datetimemodule.c	(revision 59236)
+++ Modules/datetimemodule.c	(working copy)
@@ -2058,6 +2058,28 @@
 	return Py_BuildValue("ON", Py_Type(self), delta_getstate(self));
 }
 
+static PyObject *
+delta_tosecs(PyDateTime_Delta* self)
+{
+        PyObject        *t1, *t2;
+        PyObject        *days, *secs;
+        PyObject        *retval;
+        
+        t1 = PyLong_FromLong(GET_TD_DAYS(self));
+        t2 = PyLong_FromLong(3600*24);
+        days = PyNumber_Multiply(t1, t2);
+        Py_DECREF(t1);
+        Py_DECREF(t2);
+
+        secs = PyLong_FromLong(GET_TD_SECONDS(self));
+
+        retval = PyNumber_Add(days, secs);
+        Py_DECREF(days);
+        Py_DECREF(secs);
+
+        return retval;
+}
+
 #define OFFSET(field)  offsetof(PyDateTime_Delta, field)
 
 static PyMemberDef delta_members[] = {
@@ -2077,6 +2099,8 @@
 	{"__reduce__", (PyCFunction)delta_reduce,     METH_NOARGS,
 	 PyDoc_STR("__reduce__() -> (cls, state)")},
 
+        {"tosecs", (PyCFunction)delta_tosecs,         METH_NOARGS,
+         PyDoc_STR("Total number of seconds represented by this timedelta object.")},
 	{NULL,	NULL},
 };
 
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to