Skip to content

Commit 063d0e1

Browse files
author
marat
committed
gh-137165: Add non-zero-padded Windows support for datetime.strftime
1 parent 01cc532 commit 063d0e1

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

Lib/_pydatetime.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ def _need_normalize_century():
213213
_normalize_century = True
214214
return _normalize_century
215215

216+
def _make_dash_replacement(ch, timetuple):
217+
fmt = '%' + ch
218+
val = _time.strftime(fmt, timetuple)
219+
return val.lstrip('0') or '0'
220+
216221
# Correctly substitute for %z and %Z escapes in strftime formats.
217222
def _wrap_strftime(object, format, timetuple):
218223
# Don't call utcoffset() or tzname() unless actually needed.
@@ -284,6 +289,16 @@ def _wrap_strftime(object, format, timetuple):
284289
push('{:04}'.format(year))
285290
if ch == 'F':
286291
push('-{:02}-{:02}'.format(*timetuple[1:3]))
292+
elif ch == '-':
293+
if i < n:
294+
next_ch = format[i]
295+
i += 1
296+
if sys.platform.startswith('win') or sys.platform.startswith('android'):
297+
push(_make_dash_replacement(next_ch, timetuple))
298+
else:
299+
push('%-' + next_ch)
300+
else:
301+
push('%-')
287302
else:
288303
push('%')
289304
push(ch)

Lib/test/datetimetester.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import textwrap
1515
import unittest
1616
import warnings
17+
import platform
1718

1819
from array import array
1920

@@ -1588,6 +1589,15 @@ def test_strftime(self):
15881589
self.assertEqual(t.strftime(""), "") # SF bug #761337
15891590
self.assertEqual(t.strftime('x'*1000), 'x'*1000) # SF bug #1556784
15901591

1592+
# SF bug #137165
1593+
if platform.system() == 'Darwin':
1594+
self.assertEqual(t.strftime("m:%-m d:%-d y:%-y"), "m:3 d:2 y:05")
1595+
elif platform.system() == 'Windows':
1596+
self.assertEqual(t.strftime("m:%#m d:%#d y:%#y"), "m:3 d:2 y:5")
1597+
self.assertEqual(t.strftime("m:%-m d:%-d y:%-y"), "m:3 d:2 y:5")
1598+
else:
1599+
self.assertEqual(t.strftime("m:%-m d:%-d y:%-y"), "m:3 d:2 y:5")
1600+
15911601
self.assertRaises(TypeError, t.strftime) # needs an arg
15921602
self.assertRaises(TypeError, t.strftime, "one", "two") # too many args
15931603
self.assertRaises(TypeError, t.strftime, 42) # arg wrong type
@@ -3890,6 +3900,11 @@ def test_strftime(self):
38903900
# A naive object replaces %z, %:z and %Z with empty strings.
38913901
self.assertEqual(t.strftime("'%z' '%:z' '%Z'"), "'' '' ''")
38923902

3903+
# SF bug #137165
3904+
self.assertEqual(t.strftime('%-H %-M %-S %f'), "1 2 3 000004")
3905+
if platform.system() == 'Windows':
3906+
self.assertEqual(t.strftime('%#H %#M %#S %f'), "1 2 3 000004")
3907+
38933908
# bpo-34482: Check that surrogates don't cause a crash.
38943909
try:
38953910
t.strftime('%H\ud800%M')
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"``).

0 commit comments

Comments
 (0)