]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#7413: Passing '\0' as the separator to datetime.datetime.isoformat()
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Tue, 29 Dec 2009 22:03:38 +0000 (22:03 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Tue, 29 Dec 2009 22:03:38 +0000 (22:03 +0000)
used to drop the time part of the result.

Lib/test/test_datetime.py
Misc/NEWS
Modules/datetimemodule.c

index 1fb3d1bf2009fc67206d9bc08a4d261b8dd2f343..c5d268ffff32b74e1fd53b51b801ae729b7f5a3e 100644 (file)
@@ -1180,6 +1180,7 @@ class TestDateTime(TestDate):
         self.assertEqual(t.isoformat(),    "0002-03-02T04:05:01.000123")
         self.assertEqual(t.isoformat('T'), "0002-03-02T04:05:01.000123")
         self.assertEqual(t.isoformat(' '), "0002-03-02 04:05:01.000123")
+        self.assertEqual(t.isoformat('\x00'), "0002-03-02\x0004:05:01.000123")
         # str is ISO format with the separator forced to a blank.
         self.assertEqual(str(t), "0002-03-02 04:05:01.000123")
 
index b23c672988bf18d8a5577db4acfeff8ab63c155e..26b33b2bd16c82edc011ecbb1bf246633c81e3aa 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 2?
 Core and Builtins
 -----------------
 
+- Issue #7413: Passing '\0' as the separator to datetime.datetime.isoformat()
+  used to drop the time part of the result.
+
 - Issue #1811: improve accuracy and cross-platform consistency for
   true division of integers: the result of a/b is now correctly
   rounded for ints a and b (at least on IEEE 754 platforms), and in
index 181883661913fd1a47e4525ebf4b6b98d2eaf2ea..7b9e2713f9bb04873fb69cb8647f72cd1c485f48 100644 (file)
@@ -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;