Skip to content

Commit 3074409

Browse files
fix: format the lowercase "a" meridiem token as "am"/"pm"
The formatter registered "a" as a localizable meridiem token and the parser accepts it, but _format_localizable_token had no branch for it, so it fell through and was emitted literally (format("a") returned "a"). Handle "a" alongside "A" and lowercase the result.
1 parent e06dace commit 3074409

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

‎src/pendulum/formatting/formatter.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,16 @@ def _format_localizable_token(
347347
first_day = cast("int", locale.get("translations.week_data.first_day"))
348348

349349
return locale.ordinalize((dt.day_of_week % 7 - first_day) % 7 + 1)
350-
elif token == "A":
350+
elif token in ("A", "a"):
351351
key = "translations.day_periods"
352352
if dt.hour >= 12:
353353
key += ".pm"
354354
else:
355355
key += ".am"
356356

357-
return cast("str", locale.get(key))
357+
period = cast("str", locale.get(key))
358+
359+
return period.lower() if token == "a" else period
358360
else:
359361
return token
360362

‎tests/formatting/test_formatter.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ def test_am_pm():
140140
assert f.format(d.set(hour=11), "A") == "AM"
141141

142142

143+
def test_am_pm_lowercase():
144+
f = Formatter()
145+
d = pendulum.datetime(2016, 8, 28, 23)
146+
assert f.format(d, "a") == "pm"
147+
assert f.format(d.set(hour=11), "a") == "am"
148+
149+
143150
def test_hour():
144151
f = Formatter()
145152
d = pendulum.datetime(2016, 8, 28, 7)

0 commit comments

Comments
 (0)