Skip to content

Commit 6b9c204

Browse files
mikegleenpganssle
authored andcommitted
bpo-34903: Document that some strptime formats only require 1 digit (GH-14149)
For datetime.datetime.strptime(), the leading zero for some two-digit formats is optional. This adds a footnote to the strftime/strptime documentation to reflect this fact, and adds some tests to ensure that it is true. bpo-34903
1 parent 5954334 commit 6b9c204

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

Doc/library/datetime.rst

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,7 @@ format codes.
20932093
| | where 0 is Sunday and 6 is | | |
20942094
| | Saturday. | | |
20952095
+-----------+--------------------------------+------------------------+-------+
2096-
| ``%d`` | Day of the month as a | 01, 02, ..., 31 | |
2096+
| ``%d`` | Day of the month as a | 01, 02, ..., 31 | \(9) |
20972097
| | zero-padded decimal number. | | |
20982098
+-----------+--------------------------------+------------------------+-------+
20992099
| ``%b`` | Month as locale's abbreviated || Jan, Feb, ..., Dec | \(1) |
@@ -2106,29 +2106,29 @@ format codes.
21062106
| | || Januar, Februar, ..., | |
21072107
| | | Dezember (de_DE) | |
21082108
+-----------+--------------------------------+------------------------+-------+
2109-
| ``%m`` | Month as a zero-padded | 01, 02, ..., 12 | |
2109+
| ``%m`` | Month as a zero-padded | 01, 02, ..., 12 | \(9) |
21102110
| | decimal number. | | |
21112111
+-----------+--------------------------------+------------------------+-------+
2112-
| ``%y`` | Year without century as a | 00, 01, ..., 99 | |
2112+
| ``%y`` | Year without century as a | 00, 01, ..., 99 | \(9) |
21132113
| | zero-padded decimal number. | | |
21142114
+-----------+--------------------------------+------------------------+-------+
21152115
| ``%Y`` | Year with century as a decimal | 0001, 0002, ..., 2013, | \(2) |
21162116
| | number. | 2014, ..., 9998, 9999 | |
21172117
+-----------+--------------------------------+------------------------+-------+
2118-
| ``%H`` | Hour (24-hour clock) as a | 00, 01, ..., 23 | |
2118+
| ``%H`` | Hour (24-hour clock) as a | 00, 01, ..., 23 | \(9) |
21192119
| | zero-padded decimal number. | | |
21202120
+-----------+--------------------------------+------------------------+-------+
2121-
| ``%I`` | Hour (12-hour clock) as a | 01, 02, ..., 12 | |
2121+
| ``%I`` | Hour (12-hour clock) as a | 01, 02, ..., 12 | \(9) |
21222122
| | zero-padded decimal number. | | |
21232123
+-----------+--------------------------------+------------------------+-------+
21242124
| ``%p`` | Locale's equivalent of either || AM, PM (en_US); | \(1), |
21252125
| | AM or PM. || am, pm (de_DE) | \(3) |
21262126
+-----------+--------------------------------+------------------------+-------+
2127-
| ``%M`` | Minute as a zero-padded | 00, 01, ..., 59 | |
2127+
| ``%M`` | Minute as a zero-padded | 00, 01, ..., 59 | \(9) |
21282128
| | decimal number. | | |
21292129
+-----------+--------------------------------+------------------------+-------+
2130-
| ``%S`` | Second as a zero-padded | 00, 01, ..., 59 | \(4) |
2131-
| | decimal number. | | |
2130+
| ``%S`` | Second as a zero-padded | 00, 01, ..., 59 | \(4), |
2131+
| | decimal number. | | \(9) |
21322132
+-----------+--------------------------------+------------------------+-------+
21332133
| ``%f`` | Microsecond as a decimal | 000000, 000001, ..., | \(5) |
21342134
| | number, zero-padded on the | 999999 | |
@@ -2142,19 +2142,19 @@ format codes.
21422142
| ``%Z`` | Time zone name (empty string | (empty), UTC, EST, CST | |
21432143
| | if the object is naive). | | |
21442144
+-----------+--------------------------------+------------------------+-------+
2145-
| ``%j`` | Day of the year as a | 001, 002, ..., 366 | |
2145+
| ``%j`` | Day of the year as a | 001, 002, ..., 366 | \(9) |
21462146
| | zero-padded decimal number. | | |
21472147
+-----------+--------------------------------+------------------------+-------+
2148-
| ``%U`` | Week number of the year | 00, 01, ..., 53 | \(7) |
2149-
| | (Sunday as the first day of | | |
2148+
| ``%U`` | Week number of the year | 00, 01, ..., 53 | \(7), |
2149+
| | (Sunday as the first day of | | \(9) |
21502150
| | the week) as a zero padded | | |
21512151
| | decimal number. All days in a | | |
21522152
| | new year preceding the first | | |
21532153
| | Sunday are considered to be in | | |
21542154
| | week 0. | | |
21552155
+-----------+--------------------------------+------------------------+-------+
2156-
| ``%W`` | Week number of the year | 00, 01, ..., 53 | \(7) |
2157-
| | (Monday as the first day of | | |
2156+
| ``%W`` | Week number of the year | 00, 01, ..., 53 | \(7), |
2157+
| | (Monday as the first day of | | \(9) |
21582158
| | the week) as a decimal number. | | |
21592159
| | All days in a new year | | |
21602160
| | preceding the first Monday | | |
@@ -2194,8 +2194,8 @@ incomplete or ambiguous ISO 8601 directives will raise a :exc:`ValueError`.
21942194
| ``%u`` | ISO 8601 weekday as a decimal | 1, 2, ..., 7 | |
21952195
| | number where 1 is Monday. | | |
21962196
+-----------+--------------------------------+------------------------+-------+
2197-
| ``%V`` | ISO 8601 week as a decimal | 01, 02, ..., 53 | \(8) |
2198-
| | number with Monday as | | |
2197+
| ``%V`` | ISO 8601 week as a decimal | 01, 02, ..., 53 | \(8), |
2198+
| | number with Monday as | | \(9) |
21992199
| | the first day of the week. | | |
22002200
| | Week 01 is the week containing | | |
22012201
| | Jan 4. | | |
@@ -2291,6 +2291,11 @@ Notes:
22912291
:meth:`strptime` format string. Also note that ``%G`` and ``%Y`` are not
22922292
interchangeable.
22932293

2294+
(9)
2295+
When used with the :meth:`strptime` method, the leading zero is optional
2296+
for formats ``%d``, ``%m``, ``%H``, ``%I``, ``%M``, ``%S``, ``%J``, ``%U``,
2297+
``%W``, and ``%V``. Format ``%y`` does require a leading zero.
2298+
22942299
.. rubric:: Footnotes
22952300

22962301
.. [#] If, that is, we ignore the effects of Relativity

Lib/test/datetimetester.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,6 +2501,7 @@ def test_strptime(self):
25012501
self.assertEqual(expected, got)
25022502

25032503
strptime = self.theclass.strptime
2504+
25042505
self.assertEqual(strptime("+0002", "%z").utcoffset(), 2 * MINUTE)
25052506
self.assertEqual(strptime("-0002", "%z").utcoffset(), -2 * MINUTE)
25062507
self.assertEqual(
@@ -2535,6 +2536,40 @@ def test_strptime(self):
25352536
with self.assertRaises(ValueError): strptime("-2400", "%z")
25362537
with self.assertRaises(ValueError): strptime("-000", "%z")
25372538

2539+
def test_strptime_single_digit(self):
2540+
# bpo-34903: Check that single digit dates and times are allowed.
2541+
2542+
strptime = self.theclass.strptime
2543+
2544+
with self.assertRaises(ValueError):
2545+
# %y does require two digits.
2546+
newdate = strptime('01/02/3 04:05:06', '%d/%m/%y %H:%M:%S')
2547+
dt1 = self.theclass(2003, 2, 1, 4, 5, 6)
2548+
dt2 = self.theclass(2003, 1, 2, 4, 5, 6)
2549+
dt3 = self.theclass(2003, 2, 1, 0, 0, 0)
2550+
dt4 = self.theclass(2003, 1, 25, 0, 0, 0)
2551+
inputs = [
2552+
('%d', '1/02/03 4:5:6', '%d/%m/%y %H:%M:%S', dt1),
2553+
('%m', '01/2/03 4:5:6', '%d/%m/%y %H:%M:%S', dt1),
2554+
('%H', '01/02/03 4:05:06', '%d/%m/%y %H:%M:%S', dt1),
2555+
('%M', '01/02/03 04:5:06', '%d/%m/%y %H:%M:%S', dt1),
2556+
('%S', '01/02/03 04:05:6', '%d/%m/%y %H:%M:%S', dt1),
2557+
('%j', '2/03 04am:05:06', '%j/%y %I%p:%M:%S',dt2),
2558+
('%I', '02/03 4am:05:06', '%j/%y %I%p:%M:%S',dt2),
2559+
('%w', '6/04/03', '%w/%U/%y', dt3),
2560+
# %u requires a single digit.
2561+
('%W', '6/4/2003', '%u/%W/%Y', dt3),
2562+
('%V', '6/4/2003', '%u/%V/%G', dt4),
2563+
]
2564+
for reason, string, format, target in inputs:
2565+
reason = 'test single digit ' + reason
2566+
with self.subTest(reason=reason,
2567+
string=string,
2568+
format=format,
2569+
target=target):
2570+
newdate = strptime(string, format)
2571+
self.assertEqual(newdate, target, msg=reason)
2572+
25382573
def test_more_timetuple(self):
25392574
# This tests fields beyond those tested by the TestDate.test_timetuple.
25402575
t = self.theclass(2004, 12, 31, 6, 22, 33)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Documented that in :meth:`datetime.datetime.strptime()`, the leading zero in some two-digit formats is optional. Patch by Mike Gleen.

0 commit comments

Comments
 (0)