From 307440991406f8f43a8acc24afc698db3ebd7fc2 Mon Sep 17 00:00:00 2001 From: Vansh Jenikumar Barfiwala <227775098+vanshbarfiwala-lgtm@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:56:03 +0530 Subject: [PATCH] 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. --- src/pendulum/formatting/formatter.py | 6 ++++-- tests/formatting/test_formatter.py | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/pendulum/formatting/formatter.py b/src/pendulum/formatting/formatter.py index b09977d0d..7ebdaeebc 100644 --- a/src/pendulum/formatting/formatter.py +++ b/src/pendulum/formatting/formatter.py @@ -347,14 +347,16 @@ def _format_localizable_token( first_day = cast("int", locale.get("translations.week_data.first_day")) return locale.ordinalize((dt.day_of_week % 7 - first_day) % 7 + 1) - elif token == "A": + elif token in ("A", "a"): key = "translations.day_periods" if dt.hour >= 12: key += ".pm" else: key += ".am" - return cast("str", locale.get(key)) + period = cast("str", locale.get(key)) + + return period.lower() if token == "a" else period else: return token diff --git a/tests/formatting/test_formatter.py b/tests/formatting/test_formatter.py index b83b67be8..5e19187d1 100644 --- a/tests/formatting/test_formatter.py +++ b/tests/formatting/test_formatter.py @@ -140,6 +140,13 @@ def test_am_pm(): assert f.format(d.set(hour=11), "A") == "AM" +def test_am_pm_lowercase(): + f = Formatter() + d = pendulum.datetime(2016, 8, 28, 23) + assert f.format(d, "a") == "pm" + assert f.format(d.set(hour=11), "a") == "am" + + def test_hour(): f = Formatter() d = pendulum.datetime(2016, 8, 28, 7)