diff options
author | Amaury Forgeot d'Arc <amauryfa@gmail.com> | 2009-12-29 22:03:38 (GMT) |
---|---|---|
committer | Amaury Forgeot d'Arc <amauryfa@gmail.com> | 2009-12-29 22:03:38 (GMT) |
commit | 8645a5c81fde0afe0e00e122b64f6bc586844ec3 (patch) | |
tree | f6d47e62249f6bfd1dd253deb0b82bb460c4cd65 /Modules/datetimemodule.c | |
parent | fa1ffb69c48edd7c24918407d8b6b80186463602 (diff) | |
download | cpython-8645a5c81fde0afe0e00e122b64f6bc586844ec3.zip cpython-8645a5c81fde0afe0e00e122b64f6bc586844ec3.tar.gz cpython-8645a5c81fde0afe0e00e122b64f6bc586844ec3.tar.bz2 |
#7413: Passing '\0' as the separator to datetime.datetime.isoformat()
used to drop the time part of the result.
Diffstat (limited to 'Modules/datetimemodule.c')
-rw-r--r-- | Modules/datetimemodule.c | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c index 1818836..7b9e271 100644 --- a/Modules/datetimemodule.c +++ b/Modules/datetimemodule.c @@ -1362,21 +1362,26 @@ isoformat_date(PyDateTime_Date *dt, char buffer[], int bufflen) x = PyOS_snprintf(buffer, bufflen, "%04d-%02d-%02d", GET_YEAR(dt), GET_MONTH(dt), GET_DAY(dt)); + assert(bufflen >= x); return buffer + x; } -static void +static char * isoformat_time(PyDateTime_DateTime *dt, char buffer[], int bufflen) { + int x; int us = DATE_GET_MICROSECOND(dt); - PyOS_snprintf(buffer, bufflen, - "%02d:%02d:%02d", /* 8 characters */ - DATE_GET_HOUR(dt), - DATE_GET_MINUTE(dt), - DATE_GET_SECOND(dt)); + x = PyOS_snprintf(buffer, bufflen, + "%02d:%02d:%02d", + DATE_GET_HOUR(dt), + DATE_GET_MINUTE(dt), + DATE_GET_SECOND(dt)); + assert(bufflen >= x); if (us) - PyOS_snprintf(buffer + 8, bufflen - 8, ".%06d", us); + x += PyOS_snprintf(buffer + x, bufflen - x, ".%06d", us); + assert(bufflen >= x); + return buffer + x; } /* --------------------------------------------------------------------------- @@ -4211,8 +4216,8 @@ datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) cp = isoformat_date((PyDateTime_Date *)self, buffer, sizeof(buffer)); assert(cp != NULL); *cp++ = sep; - isoformat_time(self, cp, sizeof(buffer) - (cp - buffer)); - result = PyString_FromString(buffer); + cp = isoformat_time(self, cp, sizeof(buffer) - (cp - buffer)); + result = PyString_FromStringAndSize(buffer, cp - buffer); if (result == NULL || ! HASTZINFO(self)) return result; |