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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## [Unreleased]

### Fixed
- Fixed `from_format()` with day-of-year tokens (`DDD`/`DDDD`) for years before 1000.

## [3.2.0] - 2026-01-30

### Added
Expand Down
3 changes: 2 additions & 1 deletion src/pendulum/formatting/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,10 @@ def _check_parsed(
validated["year"] = now.year

if parsed["day_of_year"] is not None:
year = cast("int", validated["year"])
dt = cast(
"pendulum.DateTime",
pendulum.parse(f"{validated['year']}-{parsed['day_of_year']:>03d}"),
pendulum.parse(f"{year:04d}-{parsed['day_of_year']:03d}"),
)

validated["month"] = dt.month
Expand Down
33 changes: 33 additions & 0 deletions tests/datetime/test_from_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,36 @@ def test_from_format_2_digit_year():

d = pendulum.from_format("99", "YY")
assert d.year == 1999


@pytest.mark.parametrize("fmt", ["YYYY-DDD", "YYYY-DDDD"])
@pytest.mark.parametrize(
"year,month,day",
[
(1, 1, 31),
(99, 12, 31),
(400, 2, 29),
(999, 1, 31),
(1000, 1, 31),
(9999, 12, 31),
],
)
def test_from_format_day_of_year_round_trip(fmt, year, month, day):
expected = pendulum.datetime(year, month, day)

assert pendulum.from_format(expected.format(fmt), fmt) == expected


def test_from_format_day_of_year_preserves_time_and_offset():
d = pendulum.from_format(
"0075-032 12:34:56.123456 +05:30", "YYYY-DDDD HH:mm:ss.SSSSSS Z"
)

assert_datetime(d, 75, 2, 1, 12, 34, 56, 123456)
assert d.utcoffset().total_seconds() == 19800


@pytest.mark.parametrize("text", ["0001-366", "0400-367", "0000-031"])
def test_from_format_day_of_year_rejects_invalid_dates(text):
with pytest.raises(ValueError):
pendulum.from_format(text, "YYYY-DDDD")