Index: Lib/test/test_datetime.py =================================================================== --- Lib/test/test_datetime.py (revision 62989) +++ Lib/test/test_datetime.py (working copy) @@ -1453,6 +1453,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 (revision 62989) +++ Modules/datetimemodule.c (working copy) @@ -4516,6 +4516,39 @@ 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(PyDateTime_DateTime *self) +{ + PyObject *timetuple = NULL; + PyObject *timestamp = NULL; + PyObject *ms_timestamp = NULL; + + timetuple = datetime_timetuple(self); + if (timetuple == NULL) + return NULL; + + PyObject *time = PyImport_ImportModuleNoBlock("time"); + if (time != NULL) { + timestamp = PyObject_CallMethod(time, "mktime", "O", timetuple); + Py_DECREF(time); + } + else + return NULL; /* failed to import time module */ + + if (timestamp == NULL) + return NULL; /* call to time.mktime(..) failed */ + + ms_timestamp = PyNumber_Add(timestamp, + PyFloat_FromDouble((double) DATE_GET_MICROSECOND(self) / 1000000.0) + ); + + return ms_timestamp; +} +#endif /* HAVE_MKTIME */ + /* Pickle support, a simple use of __reduce__. */ /* Let basestate be the non-tzinfo data string. @@ -4597,6 +4630,11 @@ {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS, PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, +#ifdef HAVE_MKTIME + {"totimestamp", (PyCFunction)datetime_totimestamp, METH_NOARGS, + PyDoc_STR("Return timestamp as seconds and milliseconds since epoch.")}, +#endif /* HAVE_MKTIME */ + {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS, PyDoc_STR("[sep] -> string in ISO 8601 format, " "YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM].\n\n"