diff --git a/docs/docs/string_formatting.md b/docs/docs/string_formatting.md index 91b95fc4..a00611e0 100644 --- a/docs/docs/string_formatting.md +++ b/docs/docs/string_formatting.md @@ -140,6 +140,7 @@ The following tokens are currently supported: | | SSSS ... | 000[0..] 001[0..] ... 998[0..] 999[0..] | | | SSSSSS | | | **AM / PM** | A | AM, PM | +| | a | am, pm | | **Timezone** | Z | -07:00, -06:00 ... +06:00, +07:00 | | | ZZ | -0700, -0600 ... +0600, +0700 | | | z | Asia/Baku, Europe/Warsaw, GMT ... | diff --git a/src/pendulum/formatting/formatter.py b/src/pendulum/formatting/formatter.py index b09977d0..dfebc363 100644 --- a/src/pendulum/formatting/formatter.py +++ b/src/pendulum/formatting/formatter.py @@ -355,6 +355,14 @@ def _format_localizable_token( key += ".am" return cast("str", locale.get(key)) + elif token == "a": + key = "translations.day_periods" + if dt.hour >= 12: + key += ".pm" + else: + key += ".am" + + return cast("str", locale.get(key)).lower() else: return token diff --git a/tests/formatting/test_formatter.py b/tests/formatting/test_formatter.py index b83b67be..5e19187d 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)