Skip to content

Commit 7678944

Browse files
committed
Fix lowercase 'a' token formatting to lowercase meridiem
The 'a' format token was returning the literal string 'a' instead of a lowercase meridiem indicator (am/pm). Only the uppercase 'A' token was handled in _format_localizable_token, so 'a' fell through to the default branch that returns the token unchanged. This also broke round-tripping: the parser already expects lowercase am/pm for the 'a' token, but formatting produced 'a' instead.
1 parent 5ad098b commit 7678944

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+
meridiem = cast("str", locale.get(key))
358+
359+
return meridiem.lower() if token == "a" else meridiem
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_lowercase_am_pm():
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)