Skip to content

Commit 224654c

Browse files
author
marat
committed
Fix 0 seconds/minutes strip and update docs
1 parent eb5c883 commit 224654c

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

Doc/library/datetime.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2764,8 +2764,9 @@ Notes:
27642764
(9)
27652765
When used with the :meth:`~.datetime.strftime` method, the leading zero is optional
27662766
for formats ``%d``, ``%m``, ``%H``, ``%I``, ``%M``, ``%S``, ``%j``, ``%U``,
2767-
``%W``, ``%V`` and ``%y`` (except that on Apple platforms, ``%y`` always produces a leading zero).
2768-
Use the ``%-`` flag to produce non-zero-padded output (e.g. ``%-d``).
2767+
``%W``, ``%V`` and ``%y`` (except that on Apple platforms, ``%y`` always produces
2768+
a leading zero). Use the ``%-`` flag to produce non-zero-padded output
2769+
(for example, ``%-d``).
27692770

27702771
(10)
27712772
When used with the :meth:`~.datetime.strptime` method, the leading zero is optional

Lib/test/datetimetester.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,7 +1589,7 @@ def test_strftime(self):
15891589
self.assertEqual(t.strftime(""), "") # SF bug #761337
15901590
self.assertEqual(t.strftime('x'*1000), 'x'*1000) # SF bug #1556784
15911591

1592-
# SF bug #137165
1592+
# See gh-137165
15931593
if platform.system() in ('Darwin', 'iOS'):
15941594
self.assertEqual(t.strftime("m:%-m d:%-d y:%-y"), "m:3 d:2 y:05")
15951595
else:
@@ -3901,11 +3901,14 @@ def test_strftime(self):
39013901
# A naive object replaces %z, %:z and %Z with empty strings.
39023902
self.assertEqual(t.strftime("'%z' '%:z' '%Z'"), "'' '' ''")
39033903

3904-
# SF bug #137165
3904+
# See gh-137165
39053905
self.assertEqual(t.strftime('%-H %-M %-S %f'), "1 2 3 000004")
39063906
if platform.system() == 'Windows':
39073907
self.assertEqual(t.strftime('%#H %#M %#S %f'), "1 2 3 000004")
39083908

3909+
t_zero = self.theclass(0, 0, 0, 4)
3910+
self.assertEqual(t_zero.strftime('%-H %-M %-S %f'), "0 0 0 000004")
3911+
39093912
# bpo-34482: Check that surrogates don't cause a crash.
39103913
try:
39113914
t.strftime('%H\ud800%M')

Modules/_datetimemodule.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,10 @@ make_dash_replacement(PyObject *object, Py_UCS4 ch, PyObject *timetuple)
18851885
goto error;
18861886
}
18871887

1888+
if (PyUnicode_GET_LENGTH(stripped) == 0) {
1889+
stripped = PyUnicode_FromString("0");
1890+
}
1891+
18881892
Py_DECREF(fmt_obj);
18891893
Py_DECREF(strftime);
18901894
Py_DECREF(res);

0 commit comments

Comments
 (0)