
python-checkins at python
Nov 25, 2009, 4:20 PM
Post #1 of 1
(70 views)
Permalink
|
|
r76530 - in python/branches/py3k: Doc/library/datetime.rst Lib/test/test_datetime.py Misc/NEWS Modules/datetimemodule.c
|
|
Author: antoine.pitrou Date: Thu Nov 26 00:02:32 2009 New Revision: 76530 Log: Merged revisions 76529 via svnmerge from svn+ssh://pythondev [at] svn/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. ........ Modified: python/branches/py3k/ (props changed) python/branches/py3k/Doc/library/datetime.rst python/branches/py3k/Lib/test/test_datetime.py python/branches/py3k/Misc/NEWS python/branches/py3k/Modules/datetimemodule.c Modified: python/branches/py3k/Doc/library/datetime.rst ============================================================================== --- python/branches/py3k/Doc/library/datetime.rst (original) +++ python/branches/py3k/Doc/library/datetime.rst Thu Nov 26 00:02:32 2009 @@ -267,12 +267,24 @@ 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 Modified: python/branches/py3k/Lib/test/test_datetime.py ============================================================================== --- python/branches/py3k/Lib/test/test_datetime.py (original) +++ python/branches/py3k/Lib/test/test_datetime.py Thu Nov 26 00:02:32 2009 @@ -261,6 +261,13 @@ 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, Modified: python/branches/py3k/Misc/NEWS ============================================================================== --- python/branches/py3k/Misc/NEWS (original) +++ python/branches/py3k/Misc/NEWS Thu Nov 26 00:02:32 2009 @@ -140,6 +140,10 @@ 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 Modified: python/branches/py3k/Modules/datetimemodule.c ============================================================================== --- python/branches/py3k/Modules/datetimemodule.c (original) +++ python/branches/py3k/Modules/datetimemodule.c Thu Nov 26 00:02:32 2009 @@ -2063,6 +2063,14 @@ } 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) { return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self)); @@ -2084,7 +2092,10 @@ }; 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}, _______________________________________________ Python-checkins mailing list Python-checkins [at] python http://mail.python.org/mailman/listinfo/python-checkins
|