[Python-checkins] cpython (merge 3.2 -> default): Followup to issue #14157: respect the relative ordering of values produced by

antoine.pitrou python-checkins at python.org
Mon May 14 19:50:50 CEST 2012


http://hg.python.org/cpython/rev/d1c0b57aeb1b
changeset:   76933:d1c0b57aeb1b
parent:      76931:c99c527f1186
parent:      76932:83598eb0d761
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Mon May 14 19:45:27 2012 +0200
summary:
  Followup to issue #14157: respect the relative ordering of values produced by time.strptime().
Patch by Hynek.

files:
  Lib/_strptime.py          |  8 ++++++++
  Lib/test/test_strptime.py |  5 +++++
  2 files changed, 13 insertions(+), 0 deletions(-)


diff --git a/Lib/_strptime.py b/Lib/_strptime.py
--- a/Lib/_strptime.py
+++ b/Lib/_strptime.py
@@ -444,8 +444,10 @@
                     else:
                         tz = value
                         break
+    leap_year_fix = False
     if year is None and month == 2 and day == 29:
         year = 1904  # 1904 is first leap year of 20th century
+        leap_year_fix = True
     elif year is None:
         year = 1900
     # If we know the week of the year and what day of that week, we can figure
@@ -476,6 +478,12 @@
     else:
         gmtoff = None
 
+    if leap_year_fix:
+        # the caller didn't supply a year but asked for Feb 29th. We couldn't
+        # use the default of 1900 for computations. We set it back to ensure
+        # that February 29th is smaller than March 1st.
+        year = 1900
+
     return (year, month, day,
             hour, minute, second,
             weekday, julian, tz, gmtoff, tzname), fraction
diff --git a/Lib/test/test_strptime.py b/Lib/test/test_strptime.py
--- a/Lib/test/test_strptime.py
+++ b/Lib/test/test_strptime.py
@@ -381,6 +381,11 @@
     def test_feb29_on_leap_year_without_year(self):
         time.strptime("Feb 29", "%b %d")
 
+    def test_mar1_comes_after_feb29_even_when_omitting_the_year(self):
+        self.assertLess(
+                time.strptime("Feb 29", "%b %d"),
+                time.strptime("Mar 1", "%b %d"))
+
 class Strptime12AMPMTests(unittest.TestCase):
     """Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list