There was an error while loading. Please reload this page.
1 parent 596d923 commit 5ae9da0Copy full SHA for 5ae9da0
3 files changed
Lib/_strptime.py
@@ -788,6 +788,9 @@ def parse_int(s):
788
datetime_result = datetime_date.fromordinal(
789
(julian - 1) +
790
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
794
year = datetime_result.year
795
month = datetime_result.month
796
day = datetime_result.day
Lib/test/test_strptime.py
@@ -873,6 +873,18 @@ def check(value, format, *expected):
873
check('2009 0 6', '%Y %W %w', 2009, 1, 3, 0, 0, 0, 5, 3)
874
check('2009 1 7', '%G %V %u', 2009, 1, 4, 0, 0, 0, 6, 4)
875
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
+
888
889
class CacheTests(unittest.TestCase):
890
"""Test that caching works properly."""
Misc/NEWS.d/next/Library/2026-09-30-01-09-20.gh-issue-73267.2gI9pf.rst
@@ -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