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
15 changes: 8 additions & 7 deletions src/pendulum/formatting/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ 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").values()
),
"dddd": "days.wide",
"ddd": "days.abbreviated",
Expand All @@ -88,12 +88,12 @@ class Formatter:
"Wo": None,
"wo": None,
"A": lambda locale: (
locale.translation("day_periods.am"),
locale.translation("day_periods.pm"),
re.escape(locale.translation("day_periods.am")),
re.escape(locale.translation("day_periods.pm")),
),
"a": lambda locale: (
locale.translation("day_periods.am").lower(),
locale.translation("day_periods.pm").lower(),
re.escape(locale.translation("day_periods.am").lower()),
re.escape(locale.translation("day_periods.pm").lower()),
),
}

Expand Down Expand Up @@ -660,7 +660,7 @@ def _get_parsed_locale_value(
raise ValueError(f'Invalid token "{token}"')

parsed[unit] = locale.match_translation(match, value)
if value is None:
if parsed[unit] is None:
raise ValueError("Invalid date")

def _replace_tokens(self, token: str, locale: Locale) -> str:
Expand All @@ -680,7 +680,8 @@ def _replace_tokens(self, token: str, locale: Locale) -> str:
candidates = values(locale)
else:
candidates = tuple(
locale.translation(
re.escape(value)
for value in locale.translation(
cast("str", self._LOCALIZABLE_TOKENS[token])
).values()
)
Expand Down
3 changes: 3 additions & 0 deletions tests/datetime/test_from_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ def test_from_format_with_locale(text, fmt, expected):
("invalid", "dd", "en"),
("invalid", "MMMM", "en"),
("invalid", "MMM", "en"),
("févrX", "MMM", "fr"),
("lunX", "ddd", "fr"),
("1X", "Do", "nb"),
],
)
def test_from_format_error(text, fmt, locale):
Expand Down