Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ def _wrap_strftime(object, format, timetuple):
push('{:04}'.format(year))
if ch == 'F':
push('-{:02}-{:02}'.format(*timetuple[1:3]))
elif ch == '-':
if i < n:
next_ch = format[i]
i += 1
if next_ch not in 'dmHIMSjUWVy':
push('%%-' + next_ch)
else:
push('%-' + next_ch)
else:
push('%-')
else:
push('%')
push(ch)
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,11 @@ def test_strftime(self):
self.assertEqual(t.strftime(""), "") # SF bug #761337
self.assertEqual(t.strftime('x'*1000), 'x'*1000) # SF bug #1556784

# unsupported %-format specifiers are passed through unchanged.
self.assertEqual(t.strftime("%-1"), "%-1")
self.assertEqual(t.strftime("%--"), "%--")
self.assertEqual(t.strftime("%-#"), "%-#")

self.assertRaises(TypeError, t.strftime) # needs an arg
self.assertRaises(TypeError, t.strftime, "one", "two") # too many args
self.assertRaises(TypeError, t.strftime, 42) # arg wrong type
Expand Down
14 changes: 14 additions & 0 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2003,6 +2003,20 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
}
continue;
}
else if (ch == '-' && i < flen) {
Py_UCS4 next_ch = PyUnicode_READ_CHAR(format, i);
i++;

if (strchr("dmHIMSjUWVy", (int)next_ch) == NULL) {
replacement = PyUnicode_FromFormat("%%%%-%c", (char)next_ch);
if (replacement == NULL) {
goto Error;
}
}
else {
continue;
}
}
else {
/* percent followed by something else */
continue;
Expand Down
Loading