Skip to content

fix: format the lowercase "a" meridiem token as "am"/"pm" - #1092

Open
vanshbarfiwala-lgtm wants to merge 1 commit into
python-pendulum:masterfrom
vanshbarfiwala-lgtm:fix/format-lowercase-meridiem
Open

vanshbarfiwala-lgtm wants to merge 1 commit into
python-pendulum:masterfrom
vanshbarfiwala-lgtm:fix/format-lowercase-meridiem

Conversation

@vanshbarfiwala-lgtm

Copy link
Copy Markdown

DateTime.format('a') emits the literal string a instead of am/pm.

import pendulum

dt = pendulum.datetime(2023, 3, 14, 15, 9)
print(dt.format('a'))        # 'a'        (expected 'pm')
print(dt.format('hh:mm a'))  # '03:09 a'  (expected '03:09 pm')
print(dt.format('A'))        # 'PM'       (uppercase already works)

The parser already accepts a, so this is a format/parse asymmetry:

pendulum.from_format('03:09 pm', 'hh:mm a').format('HH:mm')  # '15:09'  (parse works)

Cause: a is registered as a localizable meridiem token and the format regex recognizes it, but _format_localizable_token in src/pendulum/formatting/formatter.py only has a branch for A. So a falls through to else: return token and is echoed literally.

Fix: handle a alongside A and lowercase the result (English day periods are AM/PM, so a gives am/pm). Added a test next to the existing test_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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant