Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2eab5e

Browse files
committed
Followup to issue #14157: respect the relative ordering of values produced by time.strptime().
Patch by Hynek.
2 parents 4043d3d + 072e4a3 commit e2eab5e

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

Lib/_strptime.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,10 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
444444
else:
445445
tz = value
446446
break
447+
leap_year_fix = False
447448
if year is None and month == 2 and day == 29:
448449
year = 1904 # 1904 is first leap year of 20th century
450+
leap_year_fix = True
449451
elif year is None:
450452
year = 1900
451453
# If we know the week of the year and what day of that week, we can figure
@@ -476,6 +478,12 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
476478
else:
477479
gmtoff = None
478480

481+
if leap_year_fix:
482+
# the caller didn't supply a year but asked for Feb 29th. We couldn't
483+
# use the default of 1900 for computations. We set it back to ensure
484+
# that February 29th is smaller than March 1st.
485+
year = 1900
486+
479487
return (year, month, day,
480488
hour, minute, second,
481489
weekday, julian, tz, gmtoff, tzname), fraction

Lib/test/test_strptime.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,11 @@ def test_escaping(self):
381381
def test_feb29_on_leap_year_without_year(self):
382382
time.strptime("Feb 29", "%b %d")
383383

384+
def test_mar1_comes_after_feb29_even_when_omitting_the_year(self):
385+
self.assertLess(
386+
time.strptime("Feb 29", "%b %d"),
387+
time.strptime("Mar 1", "%b %d"))
388+
384389
class Strptime12AMPMTests(unittest.TestCase):
385390
"""Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""
386391

0 commit comments

Comments
 (0)