Skip to content

Commit f2029c5

Browse files
committed
Fix ordinal day parsing without locale suffixes
Signed-off-by: Aditya <adityasbakawat@gmail.com>
1 parent e06dace commit f2029c5

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎src/pendulum/formatting/formatter.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ class Formatter:
7777
"Mo": None,
7878
"DDDo": None,
7979
"Do": lambda locale: tuple(
80-
rf"\d+{o}" for o in locale.get("custom.ordinal").values()
80+
rf"\d+{re.escape(o)}"
81+
for o in (locale.get("custom.ordinal") or {"other": ""}).values()
8182
),
8283
"dddd": "days.wide",
8384
"ddd": "days.abbreviated",
@@ -676,6 +677,9 @@ def _replace_tokens(self, token: str, locale: Locale) -> str:
676677

677678
if token in self._LOCALIZABLE_TOKENS:
678679
values = self._LOCALIZABLE_TOKENS[token]
680+
if values is None:
681+
raise ValueError(f"Unsupported token: {token}")
682+
679683
if callable(values):
680684
candidates = values(locale)
681685
else:

‎tests/datetime/test_from_format.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,21 @@ def test_from_format_2_digit_year():
220220

221221
d = pendulum.from_format("99", "YY")
222222
assert d.year == 1999
223+
224+
225+
@pytest.mark.parametrize(
226+
"locale", ["de", "bg", "ru", "pl", "uk", "da", "sv", "en", "fr"]
227+
)
228+
@pytest.mark.parametrize("day", [1, 2, 3, 11, 21, 31])
229+
def test_from_format_ordinal_day_round_trip(locale, day):
230+
date = pendulum.datetime(2024, 1, day)
231+
fmt = "YYYY-MM-Do"
232+
text = date.format(fmt, locale=locale)
233+
234+
assert pendulum.from_format(text, fmt, locale=locale) == date
235+
236+
237+
@pytest.mark.parametrize("token", ["Qo", "Mo", "DDDo", "do", "e", "eo", "Wo", "wo"])
238+
def test_from_format_unsupported_localized_token(token):
239+
with pytest.raises(ValueError, match=f"^Unsupported token: {token}$"):
240+
pendulum.from_format("1", token)

0 commit comments

Comments
 (0)