Skip to content

Commit 2377e9f

Browse files
committed
#7413: Passing '\0' as the separator to datetime.datetime.isoformat()
used to drop the time part of the result.
1 parent 14970f0 commit 2377e9f

3 files changed

Lines changed: 18 additions & 9 deletions

File tree

Lib/test/test_datetime.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,7 @@ def test_isoformat(self):
11801180
self.assertEqual(t.isoformat(), "0002-03-02T04:05:01.000123")
11811181
self.assertEqual(t.isoformat('T'), "0002-03-02T04:05:01.000123")
11821182
self.assertEqual(t.isoformat(' '), "0002-03-02 04:05:01.000123")
1183+
self.assertEqual(t.isoformat('\x00'), "0002-03-02\x0004:05:01.000123")
11831184
# str is ISO format with the separator forced to a blank.
11841185
self.assertEqual(str(t), "0002-03-02 04:05:01.000123")
11851186

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 2?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #7413: Passing '\0' as the separator to datetime.datetime.isoformat()
16+
used to drop the time part of the result.
17+
1518
- Issue #1811: improve accuracy and cross-platform consistency for
1619
true division of integers: the result of a/b is now correctly
1720
rounded for ints a and b (at least on IEEE 754 platforms), and in

Modules/datetimemodule.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,21 +1362,26 @@ isoformat_date(PyDateTime_Date *dt, char buffer[], int bufflen)
13621362
x = PyOS_snprintf(buffer, bufflen,
13631363
"%04d-%02d-%02d",
13641364
GET_YEAR(dt), GET_MONTH(dt), GET_DAY(dt));
1365+
assert(bufflen >= x);
13651366
return buffer + x;
13661367
}
13671368

1368-
static void
1369+
static char *
13691370
isoformat_time(PyDateTime_DateTime *dt, char buffer[], int bufflen)
13701371
{
1372+
int x;
13711373
int us = DATE_GET_MICROSECOND(dt);
13721374

1373-
PyOS_snprintf(buffer, bufflen,
1374-
"%02d:%02d:%02d", /* 8 characters */
1375-
DATE_GET_HOUR(dt),
1376-
DATE_GET_MINUTE(dt),
1377-
DATE_GET_SECOND(dt));
1375+
x = PyOS_snprintf(buffer, bufflen,
1376+
"%02d:%02d:%02d",
1377+
DATE_GET_HOUR(dt),
1378+
DATE_GET_MINUTE(dt),
1379+
DATE_GET_SECOND(dt));
1380+
assert(bufflen >= x);
13781381
if (us)
1379-
PyOS_snprintf(buffer + 8, bufflen - 8, ".%06d", us);
1382+
x += PyOS_snprintf(buffer + x, bufflen - x, ".%06d", us);
1383+
assert(bufflen >= x);
1384+
return buffer + x;
13801385
}
13811386

13821387
/* ---------------------------------------------------------------------------
@@ -4211,8 +4216,8 @@ datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
42114216
cp = isoformat_date((PyDateTime_Date *)self, buffer, sizeof(buffer));
42124217
assert(cp != NULL);
42134218
*cp++ = sep;
4214-
isoformat_time(self, cp, sizeof(buffer) - (cp - buffer));
4215-
result = PyString_FromString(buffer);
4219+
cp = isoformat_time(self, cp, sizeof(buffer) - (cp - buffer));
4220+
result = PyString_FromStringAndSize(buffer, cp - buffer);
42164221
if (result == NULL || ! HASTZINFO(self))
42174222
return result;
42184223

0 commit comments

Comments
 (0)