]> granicus.if.org Git - python/commitdiff
Issue #23718: Fixed parsing time in week 0 before Jan 1. Original patch by
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 12 Mar 2016 08:53:09 +0000 (10:53 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 12 Mar 2016 08:53:09 +0000 (10:53 +0200)
Tamás Bence Gedai.

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

index 8da4fcde835fe874753c799b74a675b164af8cc9,f84227be4927020b38ea9d7716fcb79aa7068685..fe9436115acebbfcbd43d99ec84f526866ef2756
@@@ -503,22 -456,20 +503,26 @@@ def _strptime(data_string, format="%a %
          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
      # out the Julian day of the year.
 -    if julian is None and week_of_year != -1 and weekday is not None:
 -        week_starts_Mon = True if week_of_year_start == 0 else False
 -        julian = _calc_julian_from_U_or_W(year, week_of_year, weekday,
 -                                            week_starts_Mon)
 -        if julian <= 0:
 +    if julian is None and weekday is not None:
 +        if week_of_year is not None:
 +            week_starts_Mon = True if week_of_year_start == 0 else False
 +            julian = _calc_julian_from_U_or_W(year, week_of_year, weekday,
 +                                                week_starts_Mon)
 +        elif iso_year is not None and iso_week is not None:
 +            year, julian = _calc_julian_from_V(iso_year, iso_week, weekday + 1)
++        if julian is not None and julian <= 0:
+             year -= 1
+             yday = 366 if calendar.isleap(year) else 365
+             julian += yday
 -    # Cannot pre-calculate datetime_date() since can change in Julian
 -    # calculation and thus could have different value for the day of the week
 -    # calculation.
 +
      if julian is None:
 +        # Cannot pre-calculate datetime_date() since can change in Julian
 +        # calculation and thus could have different value for the day of
 +        # the week calculation.
          # Need to add 1 to result since first day of the year is 1, not 0.
          julian = datetime_date(year, month, day).toordinal() - \
                    datetime_date(year, 1, 1).toordinal() + 1
index 2a1f05938713d82516674d0fec1f0ec81025ea77,85126e6e53a7d1a1aa94e05e88c44a101946b785..1cc8b424a45424588e6cb3653e3add348ed2d8d5
@@@ -520,28 -496,35 +520,50 @@@ class CalculationTests(unittest.TestCas
      def test_week_0(self):
          def check(value, format, *expected):
              self.assertEqual(_strptime._strptime_time(value, format)[:-1], expected)
-         check('2015 0 0', '%Y %U %w', 2014, 12, 28, 0, 0, 0, 6, -3)
+         check('2015 0 0', '%Y %U %w', 2014, 12, 28, 0, 0, 0, 6, 362)
          check('2015 0 0', '%Y %W %w', 2015, 1, 4, 0, 0, 0, 6, 4)
-         check('2015 0 1', '%Y %U %w', 2014, 12, 29, 0, 0, 0, 0, -2)
-         check('2015 0 1', '%Y %W %w', 2014, 12, 29, 0, 0, 0, 0, -2)
 +        check('2015 1 1', '%G %V %u', 2014, 12, 29, 0, 0, 0, 0, 363)
-         check('2015 0 2', '%Y %U %w', 2014, 12, 30, 0, 0, 0, 1, -1)
-         check('2015 0 2', '%Y %W %w', 2014, 12, 30, 0, 0, 0, 1, -1)
+         check('2015 0 1', '%Y %U %w', 2014, 12, 29, 0, 0, 0, 0, 363)
+         check('2015 0 1', '%Y %W %w', 2014, 12, 29, 0, 0, 0, 0, 363)
 +        check('2015 1 2', '%G %V %u', 2014, 12, 30, 0, 0, 0, 1, 364)
-         check('2015 0 3', '%Y %U %w', 2014, 12, 31, 0, 0, 0, 2, 0)
-         check('2015 0 3', '%Y %W %w', 2014, 12, 31, 0, 0, 0, 2, 0)
+         check('2015 0 2', '%Y %U %w', 2014, 12, 30, 0, 0, 0, 1, 364)
+         check('2015 0 2', '%Y %W %w', 2014, 12, 30, 0, 0, 0, 1, 364)
 +        check('2015 1 3', '%G %V %u', 2014, 12, 31, 0, 0, 0, 2, 365)
+         check('2015 0 3', '%Y %U %w', 2014, 12, 31, 0, 0, 0, 2, 365)
+         check('2015 0 3', '%Y %W %w', 2014, 12, 31, 0, 0, 0, 2, 365)
 +        check('2015 1 4', '%G %V %u', 2015, 1, 1, 0, 0, 0, 3, 1)
          check('2015 0 4', '%Y %U %w', 2015, 1, 1, 0, 0, 0, 3, 1)
          check('2015 0 4', '%Y %W %w', 2015, 1, 1, 0, 0, 0, 3, 1)
 +        check('2015 1 5', '%G %V %u', 2015, 1, 2, 0, 0, 0, 4, 2)
          check('2015 0 5', '%Y %U %w', 2015, 1, 2, 0, 0, 0, 4, 2)
          check('2015 0 5', '%Y %W %w', 2015, 1, 2, 0, 0, 0, 4, 2)
 +        check('2015 1 6', '%G %V %u', 2015, 1, 3, 0, 0, 0, 5, 3)
          check('2015 0 6', '%Y %U %w', 2015, 1, 3, 0, 0, 0, 5, 3)
          check('2015 0 6', '%Y %W %w', 2015, 1, 3, 0, 0, 0, 5, 3)
 +        check('2015 1 7', '%G %V %u', 2015, 1, 4, 0, 0, 0, 6, 4)
  
+         check('2009 0 0', '%Y %U %w', 2008, 12, 28, 0, 0, 0, 6, 363)
+         check('2009 0 0', '%Y %W %w', 2009, 1, 4, 0, 0, 0, 6, 4)
++        check('2009 1 1', '%G %V %u', 2008, 12, 29, 0, 0, 0, 0, 364)
+         check('2009 0 1', '%Y %U %w', 2008, 12, 29, 0, 0, 0, 0, 364)
+         check('2009 0 1', '%Y %W %w', 2008, 12, 29, 0, 0, 0, 0, 364)
++        check('2009 1 2', '%G %V %u', 2008, 12, 30, 0, 0, 0, 1, 365)
+         check('2009 0 2', '%Y %U %w', 2008, 12, 30, 0, 0, 0, 1, 365)
+         check('2009 0 2', '%Y %W %w', 2008, 12, 30, 0, 0, 0, 1, 365)
++        check('2009 1 3', '%G %V %u', 2008, 12, 31, 0, 0, 0, 2, 366)
+         check('2009 0 3', '%Y %U %w', 2008, 12, 31, 0, 0, 0, 2, 366)
+         check('2009 0 3', '%Y %W %w', 2008, 12, 31, 0, 0, 0, 2, 366)
++        check('2009 1 4', '%G %V %u', 2009, 1, 1, 0, 0, 0, 3, 1)
+         check('2009 0 4', '%Y %U %w', 2009, 1, 1, 0, 0, 0, 3, 1)
+         check('2009 0 4', '%Y %W %w', 2009, 1, 1, 0, 0, 0, 3, 1)
++        check('2009 1 5', '%G %V %u', 2009, 1, 2, 0, 0, 0, 4, 2)
+         check('2009 0 5', '%Y %U %w', 2009, 1, 2, 0, 0, 0, 4, 2)
+         check('2009 0 5', '%Y %W %w', 2009, 1, 2, 0, 0, 0, 4, 2)
++        check('2009 1 6', '%G %V %u', 2009, 1, 3, 0, 0, 0, 5, 3)
+         check('2009 0 6', '%Y %U %w', 2009, 1, 3, 0, 0, 0, 5, 3)
+         check('2009 0 6', '%Y %W %w', 2009, 1, 3, 0, 0, 0, 5, 3)
++        check('2009 1 7', '%G %V %u', 2009, 1, 4, 0, 0, 0, 6, 4)
++
  
  class CacheTests(unittest.TestCase):
      """Test that caching works properly."""
diff --cc Misc/NEWS
index 1e87de899c3e7cabe8aab3fb32f0653c145a2e87,b5c34be6a1d45503a09fa6ac53bc89e6aaf1c6a4..f18a140663381678d82ee185a41e8a1f4e197c5a
+++ b/Misc/NEWS
@@@ -201,9 -90,10 +201,12 @@@ Core and Builtin
  Library
  -------
  
+ - Issue #23718: Fixed parsing time in week 0 before Jan 1.  Original patch by
+   Tamás Bence Gedai.
 +- Issue #26323: Add Mock.assert_called() and Mock.assert_called_once()
 +  methods to unittest.mock. Patch written by Amit Saha.
 +
  - Issue #20589: Invoking Path.owner() and Path.group() on Windows now raise
    NotImplementedError instead of ImportError.