Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
9 changes: 8 additions & 1 deletion Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2762,11 +2762,18 @@ Notes:
interchangeable.

(9)
When used with the :meth:`~.datetime.strftime` method, the leading zero is optional
for formats ``%d``, ``%m``, ``%H``, ``%I``, ``%M``, ``%S``, ``%j``, ``%U``,
Comment thread
mhsmith marked this conversation as resolved.
Outdated
``%W``, ``%V`` and ``%y`` (except that on Apple platforms, ``%y`` always produces
a leading zero). Use the ``%-`` flag to produce non-zero-padded output
(for example, ``%-d``).
Comment thread
mhsmith marked this conversation as resolved.
Outdated

(10)
Comment thread
mhsmith marked this conversation as resolved.
Outdated
When used with the :meth:`~.datetime.strptime` method, the leading zero is optional
for formats ``%d``, ``%m``, ``%H``, ``%I``, ``%M``, ``%S``, ``%j``, ``%U``,
``%W``, and ``%V``. Format ``%y`` does require a leading zero.

(10)
(11)
When parsing a month and day using :meth:`~.datetime.strptime`, always
include a year in the format. If the value you need to parse lacks a year,
append an explicit dummy leap year. Otherwise your code will raise an
Expand Down
11 changes: 8 additions & 3 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -1589,14 +1589,16 @@ def test_strftime(self):
self.assertEqual(t.strftime(""), "") # SF bug #761337
self.assertEqual(t.strftime('x'*1000), 'x'*1000) # SF bug #1556784

# SF bug #137165
if platform.system() == 'Darwin':
# See gh-137165
if platform.system() in ('Darwin', 'iOS'):
self.assertEqual(t.strftime("m:%-m d:%-d y:%-y"), "m:3 d:2 y:05")
Comment thread
mhsmith marked this conversation as resolved.
else:
if platform.system() == 'Windows':
self.assertEqual(t.strftime("m:%#m d:%#d y:%#y"), "m:3 d:2 y:5")
self.assertEqual(t.strftime("m:%-m d:%-d y:%-y"), "m:3 d:2 y:5")

self.assertEqual(t.strftime("%-j. %-U. %-W. %-V."), "61. 9. 9. 9.")

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 Expand Up @@ -3899,11 +3901,14 @@ def test_strftime(self):
# A naive object replaces %z, %:z and %Z with empty strings.
self.assertEqual(t.strftime("'%z' '%:z' '%Z'"), "'' '' ''")

# SF bug #137165
# See gh-137165
self.assertEqual(t.strftime('%-H %-M %-S %f'), "1 2 3 000004")
Comment thread
mhsmith marked this conversation as resolved.
if platform.system() == 'Windows':
self.assertEqual(t.strftime('%#H %#M %#S %f'), "1 2 3 000004")

t_zero = self.theclass(0, 0, 0, 4)
self.assertEqual(t_zero.strftime('%-H %-M %-S %f'), "0 0 0 000004")

# bpo-34482: Check that surrogates don't cause a crash.
try:
t.strftime('%H\ud800%M')
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Standardized non-zero-padded numeric formatting for dates and times in
:func:`datetime.datetime.strftime` and :func:`datetime.date.strftime` across
all platforms.
4 changes: 4 additions & 0 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,10 @@ make_dash_replacement(PyObject *object, Py_UCS4 ch, PyObject *timetuple)
goto error;
}

if (PyUnicode_GET_LENGTH(stripped) == 0) {
stripped = PyUnicode_FromString("0");
}
Comment thread
StanFromIreland marked this conversation as resolved.

Py_DECREF(fmt_obj);
Py_DECREF(strftime);
Py_DECREF(res);
Expand Down
Loading