Index: Modules/datetimemodule.c =================================================================== --- Modules/datetimemodule.c (revision 58132) +++ Modules/datetimemodule.c (working copy) @@ -1177,6 +1177,15 @@ return NULL; } +static PyObject * +make_freplacement(PyObject *object) +{ + char freplacement[7]; + sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object)); + + return PyBytes_FromStringAndSize(freplacement, strlen(freplacement)); +} + /* I sure don't want to reproduce the strftime code from the time module, * so this imports the module and calls it. All the hair is due to * giving special meanings to the %z and %Z format codes via a preprocessing @@ -1192,6 +1201,7 @@ PyObject *zreplacement = NULL; /* py string, replacement for %z */ PyObject *Zreplacement = NULL; /* py string, replacement for %Z */ + PyObject *freplacement = NULL; /* py string, replacement for %f */ const char *pin;/* pointer to next char in input format */ Py_ssize_t flen;/* length of input format */ @@ -1243,7 +1253,7 @@ * a new format. Since computing the replacements for those codes * is expensive, don't unless they're actually used. */ - totalnew = flen + 1; /* realistic if no %z/%Z */ + totalnew = flen + 1; /* realistic if no %z/%Z/%f */ newfmt = PyBytes_FromStringAndSize(NULL, totalnew); if (newfmt == NULL) goto Done; pnew = PyBytes_AsString(newfmt); @@ -1301,6 +1311,18 @@ ptoappend = PyBytes_AS_STRING(Zreplacement); ntoappend = PyBytes_GET_SIZE(Zreplacement); } + else if (ch == 'f') { + /* format microseconds */ + if (freplacement == NULL) { + freplacement = make_freplacement(object); + if (freplacement == NULL) + goto Done; + } + assert(freplacement != NULL); + assert(PyBytes_Check(freplacement)); + ptoappend = PyBytes_AS_STRING(freplacement); + ntoappend = PyBytes_GET_SIZE(freplacement); + } else { /* percent followed by neither z nor Z */ ptoappend = pin - 2; @@ -1347,6 +1369,7 @@ Py_DECREF(time); } Done: + Py_XDECREF(freplacement); Py_XDECREF(zreplacement); Py_XDECREF(Zreplacement); Py_XDECREF(newfmt); @@ -3159,7 +3182,7 @@ { char buf[100]; PyObject *result; - int us = TIME_GET_MICROSECOND(self);; + int us = TIME_GET_MICROSECOND(self); if (us) result = PyUnicode_FromFormat("%02d:%02d:%02d.%06d",