Fix %I/%-I strftime directives to use the 12-hour clock#182
Open
gaoflow wants to merge 2 commits into
Open
Conversation
5j9
reviewed
Jul 7, 2026
| return self.__time.hour | ||
|
|
||
| @property | ||
| def hour12(self) -> int: |
Contributor
There was a problem hiding this comment.
Since jdatetime aims for parity with Python's standard datetime module, it might be a good idea to make the hour12 property private (e.g., _hour12).
The %I and %-I directives mapped straight to the hour attribute (0-23), making them identical to %H. Per the strftime specification (and the '12' fallback already present) they are the 12-hour clock, so e.g. 1pm rendered as '13' instead of '01' and midnight as '00' instead of '12'. Add an hour12 property (hour % 12, with 0 mapped to 12) and point both directives at it. A plain date (no time) still falls back to '12'.
gaoflow
force-pushed
the
fix-strftime-I-twelve-hour
branch
from
July 15, 2026 07:59
055979f to
a37985b
Compare
Author
|
Done — |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
%Iand%-Istrftime directives map straight to thehourattribute(0–23), so they render identically to
%Hinstead of a 12-hour clock:The
'12'fallback already present in the mapping shows these are meant to bethe 12-hour clock. The reference is the stdlib:
datetime.strftime('%I').Fix
Add an
hour12property (hour % 12, with0mapped to12) and point%Iand
%-Iat it. A plaindate(no time component) still resolves to the'12'fallback, so existing date behavior is unchanged.
Tests
test_strftime_I_directive_uses_twelve_hour_clocksweeps all 24 hours andasserts
%I/%-Imatch the stdlib output, checks a%p+%Iround-tripinvariant (reconstructing the 24-hour value for every hour), spot-checks the
values the old behavior got wrong (
0→'12',13→'01',23→'11'), andthe
date-midnight→'12'fallback. Full suite: 127 passed, 3 skipped, 76subtests.
ruff check/ruff format --checkclean.