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
6 changes: 5 additions & 1 deletion src/pendulum/formatting/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ class Formatter:
"Mo": None,
"DDDo": None,
"Do": lambda locale: tuple(
rf"\d+{o}" for o in locale.get("custom.ordinal").values()
rf"\d+{re.escape(o)}"
for o in (locale.get("custom.ordinal") or {"other": ""}).values()
),
"dddd": "days.wide",
"ddd": "days.abbreviated",
Expand Down Expand Up @@ -676,6 +677,9 @@ def _replace_tokens(self, token: str, locale: Locale) -> str:

if token in self._LOCALIZABLE_TOKENS:
values = self._LOCALIZABLE_TOKENS[token]
if values is None:
raise ValueError(f"Unsupported token: {token}")

if callable(values):
candidates = values(locale)
else:
Expand Down
18 changes: 18 additions & 0 deletions tests/datetime/test_from_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,21 @@ def test_from_format_2_digit_year():

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


@pytest.mark.parametrize(
"locale", ["de", "bg", "ru", "pl", "uk", "da", "sv", "en", "fr"]
)
@pytest.mark.parametrize("day", [1, 2, 3, 11, 21, 31])
def test_from_format_ordinal_day_round_trip(locale, day):
date = pendulum.datetime(2024, 1, day)
fmt = "YYYY-MM-Do"
text = date.format(fmt, locale=locale)

assert pendulum.from_format(text, fmt, locale=locale) == date


@pytest.mark.parametrize("token", ["Qo", "Mo", "DDDo", "do", "e", "eo", "Wo", "wo"])
def test_from_format_unsupported_localized_token(token):
with pytest.raises(ValueError, match=f"^Unsupported token: {token}$"):
pendulum.from_format("1", token)