]> granicus.if.org Git - python/commitdiff
Issue #14157: Fix time.strptime failing without a year on February 29th.
authorAntoine Pitrou <solipsis@pitrou.net>
Thu, 10 May 2012 18:17:46 +0000 (20:17 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Thu, 10 May 2012 18:17:46 +0000 (20:17 +0200)
Patch by Hynek Schlawack.

Lib/_strptime.py
Lib/test/test_strptime.py
Misc/NEWS

index d9563b9e2b81a2368a9bad7fe68a2142e44c53a6..720fea851e1d67ca199e9dcf735e99e0416475c1 100644 (file)
@@ -326,7 +326,8 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
     if len(data_string) != found.end():
         raise ValueError("unconverted data remains: %s" %
                           data_string[found.end():])
-    year = 1900
+
+    year = None
     month = day = 1
     hour = minute = second = fraction = 0
     tz = -1
@@ -425,6 +426,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:
index 14927d738cd64ba2f14dfcad5f89e8978429a831..2d157f0ee2ff01d55a69e7b322fe2cf51d5385ab 100644 (file)
@@ -378,6 +378,9 @@ class StrptimeTests(unittest.TestCase):
         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)"""
 
index 7dbeb9e2292ba5a0dedd11083c06f12640b3afed..63cdad9507bf718c73eb3a166562851b35c28aec 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -60,6 +60,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 #13183: Fix pdb skipping frames after hitting a breakpoint and running