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
7 changes: 7 additions & 0 deletions src/pendulum/formatting/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class Formatter:

_FROM_FORMAT_RE: re.Pattern[str] = re.compile(r"(?<!\\\[)" + _TOKENS + r"(?!\\\])")

# Names are matched regardless of case, as in "14nov22" or "MONDAY".
_CASE_INSENSITIVE_TOKENS: ClassVar[frozenset[str]] = frozenset(
{"MMMM", "MMM", "dddd", "ddd", "dd"}
)

_LOCALIZABLE_TOKENS: ClassVar[
dict[str, str | Callable[[Locale], Sequence[str]] | None]
] = {
Expand Down Expand Up @@ -694,5 +699,7 @@ def _replace_tokens(self, token: str, locale: Locale) -> str:
candidates = (cast("str", candidates),)

pattern = f"(?P<{token}>{'|'.join(candidates)})"
if token in self._CASE_INSENSITIVE_TOKENS:
pattern = f"(?i:{pattern})"

return pattern
5 changes: 3 additions & 2 deletions src/pendulum/locales/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ def ordinalize(self, number: int) -> str:

def match_translation(self, key: str, value: Any) -> dict[str, str] | None:
translations = self.translation(key)
if value not in translations.values():
by_value = {v.lower(): k for k, v in translations.items()}
if value.lower() not in by_value:
return None

return cast(Dict[str, str], {v: k for k, v in translations.items()}[value])
return cast(Dict[str, str], by_value[value.lower()])

def __repr__(self) -> str:
return f"{self.__class__.__name__}('{self._locale}')"
7 changes: 7 additions & 0 deletions tests/datetime/test_from_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ def test_from_format_with_invalid_padded_day():
("1", "E", "2018-01-29T00:00:00+00:00", "2018-02-02"),
("March", "MMMM", "2018-03-01T00:00:00+00:00", "2018-02-02"),
("Mar", "MMM", "2018-03-01T00:00:00+00:00", "2018-02-02"),
("mar", "MMM", "2018-03-01T00:00:00+00:00", "2018-02-02"),
("MARCH", "MMMM", "2018-03-01T00:00:00+00:00", "2018-02-02"),
("14nov22", "DDMMMYY", "2022-11-14T00:00:00+00:00", None),
("monday", "dddd", "2018-01-29T00:00:00+00:00", "2018-02-02"),
("MON", "ddd", "2018-01-29T00:00:00+00:00", "2018-02-02"),
(
"Thursday 25th December 1975 02:15:16 PM",
"dddd Do MMMM YYYY hh:mm:ss A",
Expand Down Expand Up @@ -164,6 +169,8 @@ def test_from_format(text, fmt, expected, now):
("lu", "dd", "2018-01-29T00:00:00+00:00"),
("mars", "MMMM", "2018-03-01T00:00:00+00:00"),
("mars", "MMM", "2018-03-01T00:00:00+00:00"),
("Mars", "MMMM", "2018-03-01T00:00:00+00:00"),
("LUNDI", "dddd", "2018-01-29T00:00:00+00:00"),
],
)
def test_from_format_with_locale(text, fmt, expected):
Expand Down