Skip to content

Commit 67870df

Browse files
author
marat
committed
gh-137165: Add non-zero-padded Windows support
1 parent 01cc532 commit 67870df

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Lib/test/datetimetester.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,11 @@ def test_strftime(self):
15871587
self.assertEqual(t.strftime("m:%m d:%d y:%y"), "m:03 d:02 y:05")
15881588
self.assertEqual(t.strftime(""), "") # SF bug #761337
15891589
self.assertEqual(t.strftime('x'*1000), 'x'*1000) # SF bug #1556784
1590+
self.assertEqual(t.strftime("m:%-m d:%-d"), "m:3 d:2") # SF bug #137165
1591+
if sys.platform.startswith('darwin'): # SF bug #137165
1592+
self.assertEqual(t.strftime("m:%-m d:%-d y:%-y"), "m:3 d:2 y:05")
1593+
else:
1594+
self.assertEqual(t.strftime("m:%-m d:%-d y:%-y"), "m:3 d:2 y:5")
15901595

15911596
self.assertRaises(TypeError, t.strftime) # needs an arg
15921597
self.assertRaises(TypeError, t.strftime, "one", "two") # too many args
@@ -3889,6 +3894,7 @@ def test_strftime(self):
38893894
self.assertEqual(t.strftime('%H %M %S %f'), "01 02 03 000004")
38903895
# A naive object replaces %z, %:z and %Z with empty strings.
38913896
self.assertEqual(t.strftime("'%z' '%:z' '%Z'"), "'' '' ''")
3897+
self.assertEqual(t.strftime('%-H %-M %-S %f'), "1 2 3 000004") # SF bug #137165
38923898

38933899
# bpo-34482: Check that surrogates don't cause a crash.
38943900
try:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add support for Windows non-zero-padded formatting directives in
2+
:func:`datetime.datetime.strftime` (e.g., ``"m:%-m d:%-d y:%-y"``).

Modules/_datetimemodule.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,21 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
20022002
}
20032003
continue;
20042004
}
2005+
#ifdef MS_WINDOWS
2006+
/* non-0-pad Windows support */
2007+
else if (ch == '-' && i < flen) {
2008+
Py_UCS4 next_ch = PyUnicode_READ_CHAR(format, i);
2009+
if (strchr("dHImMSUwWyY", next_ch) != NULL) {
2010+
i++;
2011+
replacement = PyUnicode_FromFormat("%s%c", "%#", next_ch);
2012+
if (replacement == NULL) {
2013+
goto Error;
2014+
}
2015+
} else {
2016+
continue;
2017+
}
2018+
}
2019+
#endif
20052020
else {
20062021
/* percent followed by something else */
20072022
continue;

0 commit comments

Comments
 (0)