Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions arrow/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,13 @@ def _build_datetime(parts: _Parts) -> datetime:
f"The provided day of year {day_of_year!r} is invalid."
)

# strptime accepts day 366 in a non-leap year and rolls the result
# over into the next year instead of rejecting it; guard that here.
if dt.year != int(_year):
raise ParserError(
f"The provided day of year {day_of_year!r} is invalid."
)

parts["year"] = dt.year
parts["month"] = dt.month
parts["day"] = dt.day
Expand Down
6 changes: 4 additions & 2 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,8 +939,10 @@ def test_YYYY_DDDD(self):
# Since 2016 is a leap year, the 366th day falls in the same year
assert self.parser.parse_iso("2016-366") == datetime(2016, 12, 31)

# Since 2017 is not a leap year, the 366th day falls in the next year
assert self.parser.parse_iso("2017-366") == datetime(2018, 1, 1)
# 2017 is not a leap year, so its 366th day does not exist and is
# rejected rather than rolling over into the next year
with pytest.raises(ParserError):
self.parser.parse_iso("2017-366")

def test_YYYY_DDDD_HH_mm_ssZ(self):
assert self.parser.parse_iso("2013-036 04:05:06+01:00") == datetime(
Expand Down
Loading