Skip to content

Commit 1682e5d

Browse files
committed
Issue #14157: Fix time.strptime failing without a year on February 29th.
Patch by Hynek Schlawack.
1 parent a9494f6 commit 1682e5d

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

Lib/_strptime.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
339339
raise ValueError("unconverted data remains: %s" %
340340
data_string[found.end():])
341341

342-
year = 1900
342+
year = None
343343
month = day = 1
344344
hour = minute = second = fraction = 0
345345
tz = -1
@@ -444,6 +444,10 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
444444
else:
445445
tz = value
446446
break
447+
if year is None and month == 2 and day == 29:
448+
year = 1904 # 1904 is first leap year of 20th century
449+
elif year is None:
450+
year = 1900
447451
# If we know the week of the year and what day of that week, we can figure
448452
# out the Julian day of the year.
449453
if julian == -1 and week_of_year != -1 and weekday != -1:

Lib/test/test_strptime.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,9 @@ def test_escaping(self):
378378
need_escaping = ".^$*+?{}\[]|)("
379379
self.assertTrue(_strptime._strptime_time(need_escaping, need_escaping))
380380

381+
def test_feb29_on_leap_year_without_year(self):
382+
time.strptime("Feb 29", "%b %d")
383+
381384
class Strptime12AMPMTests(unittest.TestCase):
382385
"""Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""
383386

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ Core and Builtins
6363
Library
6464
-------
6565

66+
- Issue #14157: Fix time.strptime failing without a year on February 29th.
67+
Patch by Hynek Schlawack.
68+
6669
- Issue #14768: os.path.expanduser('~/a') doesn't works correctly when HOME is '/'.
6770

6871
- Issue #14741: Fix missing support for Ellipsis ('...') in parser module.

0 commit comments

Comments
 (0)