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

Skip to content

Commit 8a7240e

Browse files
Issue #23718: Fixed parsing time in week 0 before Jan 1. Original patch by
Tamás Bence Gedai.
1 parent d756963 commit 8a7240e

4 files changed

Lines changed: 29 additions & 7 deletions

File tree

Lib/_strptime.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,10 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
462462
week_starts_Mon = True if week_of_year_start == 0 else False
463463
julian = _calc_julian_from_U_or_W(year, week_of_year, weekday,
464464
week_starts_Mon)
465+
if julian <= 0:
466+
year -= 1
467+
yday = 366 if calendar.isleap(year) else 365
468+
julian += yday
465469
# Cannot pre-calculate datetime_date() since can change in Julian
466470
# calculation and thus could have different value for the day of the week
467471
# calculation.

Lib/test/test_strptime.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -496,21 +496,35 @@ def test_helper(ymd_tuple, test_reason):
496496
def test_week_0(self):
497497
def check(value, format, *expected):
498498
self.assertEqual(_strptime._strptime_time(value, format)[:-1], expected)
499-
check('2015 0 0', '%Y %U %w', 2014, 12, 28, 0, 0, 0, 6, -3)
499+
check('2015 0 0', '%Y %U %w', 2014, 12, 28, 0, 0, 0, 6, 362)
500500
check('2015 0 0', '%Y %W %w', 2015, 1, 4, 0, 0, 0, 6, 4)
501-
check('2015 0 1', '%Y %U %w', 2014, 12, 29, 0, 0, 0, 0, -2)
502-
check('2015 0 1', '%Y %W %w', 2014, 12, 29, 0, 0, 0, 0, -2)
503-
check('2015 0 2', '%Y %U %w', 2014, 12, 30, 0, 0, 0, 1, -1)
504-
check('2015 0 2', '%Y %W %w', 2014, 12, 30, 0, 0, 0, 1, -1)
505-
check('2015 0 3', '%Y %U %w', 2014, 12, 31, 0, 0, 0, 2, 0)
506-
check('2015 0 3', '%Y %W %w', 2014, 12, 31, 0, 0, 0, 2, 0)
501+
check('2015 0 1', '%Y %U %w', 2014, 12, 29, 0, 0, 0, 0, 363)
502+
check('2015 0 1', '%Y %W %w', 2014, 12, 29, 0, 0, 0, 0, 363)
503+
check('2015 0 2', '%Y %U %w', 2014, 12, 30, 0, 0, 0, 1, 364)
504+
check('2015 0 2', '%Y %W %w', 2014, 12, 30, 0, 0, 0, 1, 364)
505+
check('2015 0 3', '%Y %U %w', 2014, 12, 31, 0, 0, 0, 2, 365)
506+
check('2015 0 3', '%Y %W %w', 2014, 12, 31, 0, 0, 0, 2, 365)
507507
check('2015 0 4', '%Y %U %w', 2015, 1, 1, 0, 0, 0, 3, 1)
508508
check('2015 0 4', '%Y %W %w', 2015, 1, 1, 0, 0, 0, 3, 1)
509509
check('2015 0 5', '%Y %U %w', 2015, 1, 2, 0, 0, 0, 4, 2)
510510
check('2015 0 5', '%Y %W %w', 2015, 1, 2, 0, 0, 0, 4, 2)
511511
check('2015 0 6', '%Y %U %w', 2015, 1, 3, 0, 0, 0, 5, 3)
512512
check('2015 0 6', '%Y %W %w', 2015, 1, 3, 0, 0, 0, 5, 3)
513513

514+
check('2009 0 0', '%Y %U %w', 2008, 12, 28, 0, 0, 0, 6, 363)
515+
check('2009 0 0', '%Y %W %w', 2009, 1, 4, 0, 0, 0, 6, 4)
516+
check('2009 0 1', '%Y %U %w', 2008, 12, 29, 0, 0, 0, 0, 364)
517+
check('2009 0 1', '%Y %W %w', 2008, 12, 29, 0, 0, 0, 0, 364)
518+
check('2009 0 2', '%Y %U %w', 2008, 12, 30, 0, 0, 0, 1, 365)
519+
check('2009 0 2', '%Y %W %w', 2008, 12, 30, 0, 0, 0, 1, 365)
520+
check('2009 0 3', '%Y %U %w', 2008, 12, 31, 0, 0, 0, 2, 366)
521+
check('2009 0 3', '%Y %W %w', 2008, 12, 31, 0, 0, 0, 2, 366)
522+
check('2009 0 4', '%Y %U %w', 2009, 1, 1, 0, 0, 0, 3, 1)
523+
check('2009 0 4', '%Y %W %w', 2009, 1, 1, 0, 0, 0, 3, 1)
524+
check('2009 0 5', '%Y %U %w', 2009, 1, 2, 0, 0, 0, 4, 2)
525+
check('2009 0 5', '%Y %W %w', 2009, 1, 2, 0, 0, 0, 4, 2)
526+
check('2009 0 6', '%Y %U %w', 2009, 1, 3, 0, 0, 0, 5, 3)
527+
check('2009 0 6', '%Y %W %w', 2009, 1, 3, 0, 0, 0, 5, 3)
514528

515529
class CacheTests(unittest.TestCase):
516530
"""Test that caching works properly."""

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ Matthieu Gautier
484484
Stephen M. Gava
485485
Xavier de Gaye
486486
Harry Henry Gebel
487+
Tamás Bence Gedai
487488
Marius Gedminas
488489
Jan-Philip Gehrcke
489490
Thomas Gellekum

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ Core and Builtins
9191
Library
9292
-------
9393

94+
- Issue #23718: Fixed parsing time in week 0 before Jan 1. Original patch by
95+
Tamás Bence Gedai.
96+
9497
- Issue #20589: Invoking Path.owner() and Path.group() on Windows now raise
9598
NotImplementedError instead of ImportError.
9699

0 commit comments

Comments
 (0)