Skip to content

Commit

Permalink
Issue #14157: Fix time.strptime failing without a year on February 29th.
Browse files Browse the repository at this point in the history
Patch by Hynek Schlawack.
  • Loading branch information
pitrou committed May 10, 2012
1 parent a9494f6 commit 1682e5d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Lib/_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
raise ValueError("unconverted data remains: %s" %
data_string[found.end():])

year = 1900
year = None
month = day = 1
hour = minute = second = fraction = 0
tz = -1
Expand Down Expand Up @@ -444,6 +444,10 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
else:
tz = value
break
if year is None and month == 2 and day == 29:
year = 1904 # 1904 is first leap year of 20th century
elif year is None:
year = 1900
# If we know the week of the year and what day of that week, we can figure
# out the Julian day of the year.
if julian == -1 and week_of_year != -1 and weekday != -1:
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ def test_escaping(self):
need_escaping = ".^$*+?{}\[]|)("
self.assertTrue(_strptime._strptime_time(need_escaping, need_escaping))

def test_feb29_on_leap_year_without_year(self):
time.strptime("Feb 29", "%b %d")

class Strptime12AMPMTests(unittest.TestCase):
"""Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""

Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ Core and Builtins
Library
-------

- Issue #14157: Fix time.strptime failing without a year on February 29th.
Patch by Hynek Schlawack.

- Issue #14768: os.path.expanduser('~/a') doesn't works correctly when HOME is '/'.

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

0 comments on commit 1682e5d

Please sign in to comment.