]> granicus.if.org Git - python/commitdiff
Followup to issue #14157: respect the relative ordering of values produced by time...
authorAntoine Pitrou <solipsis@pitrou.net>
Mon, 14 May 2012 17:44:59 +0000 (19:44 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Mon, 14 May 2012 17:44:59 +0000 (19:44 +0200)
Patch by Hynek.

Lib/_strptime.py
Lib/test/test_strptime.py

index 720fea851e1d67ca199e9dcf735e99e0416475c1..2df30a22ea5e8aaa35c742e0c5c76f80e91b6dc5 100644 (file)
@@ -426,8 +426,10 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
                     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
@@ -451,6 +453,12 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
         day = datetime_result.day
     if weekday == -1:
         weekday = datetime_date(year, month, day).weekday()
+    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 (time.struct_time((year, month, day,
                               hour, minute, second,
                               weekday, julian, tz)), fraction)
index 2d157f0ee2ff01d55a69e7b322fe2cf51d5385ab..58e4309a0d76ccac74c28012b080839725a85372 100644 (file)
@@ -381,6 +381,11 @@ class StrptimeTests(unittest.TestCase):
     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)"""