Index: Doc/library/datetime.rst =================================================================== --- Doc/library/datetime.rst (révision 67177) +++ Doc/library/datetime.rst (copie de travail) @@ -925,6 +925,12 @@ boundary. +.. method:: datetime.totimestamp() + + Return timestamp of the datetime as seconds and milliseconds since the epoch. + The same as ``time.mktime(self.timetuple()) + (self.microsecond / 1000000.0)``. + + .. method:: datetime.toordinal() Return the proleptic Gregorian ordinal of the date. The same as Index: Lib/test/test_datetime.py =================================================================== --- Lib/test/test_datetime.py (révision 67177) +++ Lib/test/test_datetime.py (copie de travail) @@ -1458,6 +1458,16 @@ got = self.theclass.utcfromtimestamp(ts) self.verify_field_equality(expected, got) + def test_totimestamp(self): + import time + + now = self.theclass.now() + seconds = time.mktime(now.timetuple()) + seconds += now.microsecond / 1000000.0 + timestamp = now.totimestamp() + + self.assertEquals(seconds, timestamp) + def test_microsecond_rounding(self): # Test whether fromtimestamp "rounds up" floats that are less # than one microsecond smaller than an integer. Index: Modules/datetimemodule.c =================================================================== --- Modules/datetimemodule.c (révision 67177) +++ Modules/datetimemodule.c (copie de travail) @@ -4527,6 +4527,41 @@ return build_struct_time(y, m, d, hh, mm, ss, 0); } +#ifdef HAVE_MKTIME + +/* datetime_totimestamp will fail if time.mktime(...) is not defined. */ +static PyObject * +datetime_totimestamp(PyObject *self) +{ + PyObject *timetuple, *time; + PyObject *timet, *us, *timestamp; + + /* create the time tuple */ + timetuple = datetime_timetuple((PyDateTime_DateTime *)self); + if (timetuple == NULL) + return NULL; + + /* call time.mktime(tuple) */ + time = PyImport_ImportModuleNoBlock("time"); + if (time == NULL) + return NULL; + timet = PyObject_CallMethod(time, "mktime", "O", timetuple); + Py_DECREF(timetuple); + Py_DECREF(time); + if (timet == NULL) + return NULL; + + /* add microseconds / 1e6 */ + us = PyFloat_FromDouble(DATE_GET_MICROSECOND(self) * 1e-6); + timestamp = PyNumber_Add(timet, us); + Py_DECREF(timet); + Py_DECREF(us); + + return timestamp; +} + +#endif + /* Pickle support, a simple use of __reduce__. */ /* Let basestate be the non-tzinfo data string. @@ -4579,6 +4614,12 @@ PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp " "(like time.time()).")}, +#ifdef HAVE_MKTIME + {"totimestamp", (PyCFunction)datetime_totimestamp, + METH_NOARGS, + PyDoc_STR("Return timestamp as seconds and milliseconds since epoch.")}, +#endif + {"strptime", (PyCFunction)datetime_strptime, METH_VARARGS | METH_CLASS, PyDoc_STR("string, format -> new datetime parsed from a string "