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

Skip to content

Commit 6e4150f

Browse files
Issue #23718: Fixed parsing time in week 0 before Jan 1. Original patch by
Tamás Bence Gedai.
2 parents 15a83e8 + 8a7240e commit 6e4150f

3 files changed

Lines changed: 36 additions & 7 deletions

File tree

Lib/_strptime.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,10 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
514514
week_starts_Mon)
515515
elif iso_year is not None and iso_week is not None:
516516
year, julian = _calc_julian_from_V(iso_year, iso_week, weekday + 1)
517+
if julian is not None and julian <= 0:
518+
year -= 1
519+
yday = 366 if calendar.isleap(year) else 365
520+
julian += yday
517521

518522
if julian is None:
519523
# Cannot pre-calculate datetime_date() since can change in Julian

Lib/test/test_strptime.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -520,17 +520,17 @@ def test_helper(ymd_tuple, test_reason):
520520
def test_week_0(self):
521521
def check(value, format, *expected):
522522
self.assertEqual(_strptime._strptime_time(value, format)[:-1], expected)
523-
check('2015 0 0', '%Y %U %w', 2014, 12, 28, 0, 0, 0, 6, -3)
523+
check('2015 0 0', '%Y %U %w', 2014, 12, 28, 0, 0, 0, 6, 362)
524524
check('2015 0 0', '%Y %W %w', 2015, 1, 4, 0, 0, 0, 6, 4)
525525
check('2015 1 1', '%G %V %u', 2014, 12, 29, 0, 0, 0, 0, 363)
526-
check('2015 0 1', '%Y %U %w', 2014, 12, 29, 0, 0, 0, 0, -2)
527-
check('2015 0 1', '%Y %W %w', 2014, 12, 29, 0, 0, 0, 0, -2)
526+
check('2015 0 1', '%Y %U %w', 2014, 12, 29, 0, 0, 0, 0, 363)
527+
check('2015 0 1', '%Y %W %w', 2014, 12, 29, 0, 0, 0, 0, 363)
528528
check('2015 1 2', '%G %V %u', 2014, 12, 30, 0, 0, 0, 1, 364)
529-
check('2015 0 2', '%Y %U %w', 2014, 12, 30, 0, 0, 0, 1, -1)
530-
check('2015 0 2', '%Y %W %w', 2014, 12, 30, 0, 0, 0, 1, -1)
529+
check('2015 0 2', '%Y %U %w', 2014, 12, 30, 0, 0, 0, 1, 364)
530+
check('2015 0 2', '%Y %W %w', 2014, 12, 30, 0, 0, 0, 1, 364)
531531
check('2015 1 3', '%G %V %u', 2014, 12, 31, 0, 0, 0, 2, 365)
532-
check('2015 0 3', '%Y %U %w', 2014, 12, 31, 0, 0, 0, 2, 0)
533-
check('2015 0 3', '%Y %W %w', 2014, 12, 31, 0, 0, 0, 2, 0)
532+
check('2015 0 3', '%Y %U %w', 2014, 12, 31, 0, 0, 0, 2, 365)
533+
check('2015 0 3', '%Y %W %w', 2014, 12, 31, 0, 0, 0, 2, 365)
534534
check('2015 1 4', '%G %V %u', 2015, 1, 1, 0, 0, 0, 3, 1)
535535
check('2015 0 4', '%Y %U %w', 2015, 1, 1, 0, 0, 0, 3, 1)
536536
check('2015 0 4', '%Y %W %w', 2015, 1, 1, 0, 0, 0, 3, 1)
@@ -542,6 +542,28 @@ def check(value, format, *expected):
542542
check('2015 0 6', '%Y %W %w', 2015, 1, 3, 0, 0, 0, 5, 3)
543543
check('2015 1 7', '%G %V %u', 2015, 1, 4, 0, 0, 0, 6, 4)
544544

545+
check('2009 0 0', '%Y %U %w', 2008, 12, 28, 0, 0, 0, 6, 363)
546+
check('2009 0 0', '%Y %W %w', 2009, 1, 4, 0, 0, 0, 6, 4)
547+
check('2009 1 1', '%G %V %u', 2008, 12, 29, 0, 0, 0, 0, 364)
548+
check('2009 0 1', '%Y %U %w', 2008, 12, 29, 0, 0, 0, 0, 364)
549+
check('2009 0 1', '%Y %W %w', 2008, 12, 29, 0, 0, 0, 0, 364)
550+
check('2009 1 2', '%G %V %u', 2008, 12, 30, 0, 0, 0, 1, 365)
551+
check('2009 0 2', '%Y %U %w', 2008, 12, 30, 0, 0, 0, 1, 365)
552+
check('2009 0 2', '%Y %W %w', 2008, 12, 30, 0, 0, 0, 1, 365)
553+
check('2009 1 3', '%G %V %u', 2008, 12, 31, 0, 0, 0, 2, 366)
554+
check('2009 0 3', '%Y %U %w', 2008, 12, 31, 0, 0, 0, 2, 366)
555+
check('2009 0 3', '%Y %W %w', 2008, 12, 31, 0, 0, 0, 2, 366)
556+
check('2009 1 4', '%G %V %u', 2009, 1, 1, 0, 0, 0, 3, 1)
557+
check('2009 0 4', '%Y %U %w', 2009, 1, 1, 0, 0, 0, 3, 1)
558+
check('2009 0 4', '%Y %W %w', 2009, 1, 1, 0, 0, 0, 3, 1)
559+
check('2009 1 5', '%G %V %u', 2009, 1, 2, 0, 0, 0, 4, 2)
560+
check('2009 0 5', '%Y %U %w', 2009, 1, 2, 0, 0, 0, 4, 2)
561+
check('2009 0 5', '%Y %W %w', 2009, 1, 2, 0, 0, 0, 4, 2)
562+
check('2009 1 6', '%G %V %u', 2009, 1, 3, 0, 0, 0, 5, 3)
563+
check('2009 0 6', '%Y %U %w', 2009, 1, 3, 0, 0, 0, 5, 3)
564+
check('2009 0 6', '%Y %W %w', 2009, 1, 3, 0, 0, 0, 5, 3)
565+
check('2009 1 7', '%G %V %u', 2009, 1, 4, 0, 0, 0, 6, 4)
566+
545567

546568
class CacheTests(unittest.TestCase):
547569
"""Test that caching works properly."""

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ Core and Builtins
201201
Library
202202
-------
203203

204+
- Issue #23718: Fixed parsing time in week 0 before Jan 1. Original patch by
205+
Tamás Bence Gedai.
206+
204207
- Issue #26323: Add Mock.assert_called() and Mock.assert_called_once()
205208
methods to unittest.mock. Patch written by Amit Saha.
206209

0 commit comments

Comments
 (0)