Skip to content

Commit 3091abe

Browse files
committed
Fix ordinal parsing fallback for partial locale suffix mappings
1 parent f2029c5 commit 3091abe

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

‎src/pendulum/formatting/formatter.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,13 @@ class Formatter:
7676
"MMM": "months.abbreviated",
7777
"Mo": None,
7878
"DDDo": None,
79-
"Do": lambda locale: tuple(
80-
rf"\d+{re.escape(o)}"
81-
for o in (locale.get("custom.ordinal") or {"other": ""}).values()
79+
"Do": lambda locale: (
80+
*(
81+
rf"\d+{re.escape(o)}"
82+
for o in (locale.get("custom.ordinal") or {}).values()
83+
if o
84+
),
85+
r"\d+",
8286
),
8387
"dddd": "days.wide",
8488
"ddd": "days.abbreviated",

‎tests/datetime/test_from_format.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,14 @@ def test_from_format_2_digit_year():
223223

224224

225225
@pytest.mark.parametrize(
226-
"locale", ["de", "bg", "ru", "pl", "uk", "da", "sv", "en", "fr"]
226+
"locale", ["de", "bg", "ru", "pl", "uk", "da", "sv", "en", "fr", "it"]
227227
)
228-
@pytest.mark.parametrize("day", [1, 2, 3, 11, 21, 31])
229-
def test_from_format_ordinal_day_round_trip(locale, day):
228+
@pytest.mark.parametrize("day", [1, 2, 3, 8, 11, 21, 31])
229+
@pytest.mark.parametrize(
230+
"fmt", ["YYYY-MM-Do", "Do MMMM YYYY", "MMMM Do, YYYY", "Do.MM.YYYY"]
231+
)
232+
def test_from_format_ordinal_day_round_trip(locale, day, fmt):
230233
date = pendulum.datetime(2024, 1, day)
231-
fmt = "YYYY-MM-Do"
232234
text = date.format(fmt, locale=locale)
233235

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

0 commit comments

Comments
 (0)