Skip to content

Commit 5ae9da0

Browse files
committed
gh-73267: Normalize strptime day of year after rollover
1 parent 596d923 commit 5ae9da0

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

‎Lib/_strptime.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,9 @@ def parse_int(s):
788788
datetime_result = datetime_date.fromordinal(
789789
(julian - 1) +
790790
datetime_date(year, 1, 1).toordinal())
791+
if year != datetime_result.year:
792+
year_start = datetime_date(datetime_result.year, 1, 1)
793+
julian = datetime_result.toordinal() - year_start.toordinal() + 1
791794
year = datetime_result.year
792795
month = datetime_result.month
793796
day = datetime_result.day

‎Lib/test/test_strptime.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,18 @@ def check(value, format, *expected):
873873
check('2009 0 6', '%Y %W %w', 2009, 1, 3, 0, 0, 0, 5, 3)
874874
check('2009 1 7', '%G %V %u', 2009, 1, 4, 0, 0, 0, 6, 4)
875875

876+
def test_julian_day_next_year(self):
877+
for value, format, expected in (
878+
('2016 52 0', '%Y %W %w', (2017, 1, 1, 6, 1)),
879+
('2015 52 5', '%Y %U %w', (2016, 1, 1, 4, 1)),
880+
('2019 366', '%Y %j', (2020, 1, 1, 2, 1)),
881+
):
882+
with self.subTest(value=value, format=format):
883+
result = time.strptime(value, format)
884+
self.assertEqual(
885+
(result.tm_year, result.tm_mon, result.tm_mday,
886+
result.tm_wday, result.tm_yday), expected)
887+
876888

877889
class CacheTests(unittest.TestCase):
878890
"""Test that caching works properly."""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`time.strptime` returning an inconsistent day of year when a week
2+
number or ordinal day resolves to a date in the following year.

0 commit comments

Comments
 (0)