Skip to content

Fix %I/%-I strftime directives to use the 12-hour clock#182

Open
gaoflow wants to merge 2 commits into
slashmili:mainfrom
gaoflow:fix-strftime-I-twelve-hour
Open

Fix %I/%-I strftime directives to use the 12-hour clock#182
gaoflow wants to merge 2 commits into
slashmili:mainfrom
gaoflow:fix-strftime-I-twelve-hour

Conversation

@gaoflow

@gaoflow gaoflow commented Jul 7, 2026

Copy link
Copy Markdown

Problem

The %I and %-I strftime directives map straight to the hour attribute
(0–23), so they render identically to %H instead of a 12-hour clock:

>>> import jdatetime
>>> jdatetime.datetime(1400, 1, 1, 13, 0).strftime('%I')   # 1pm
'13'      # should be '01'
>>> jdatetime.datetime(1400, 1, 1, 0, 0).strftime('%I')    # midnight
'00'      # should be '12'

The '12' fallback already present in the mapping shows these are meant to be
the 12-hour clock. The reference is the stdlib: datetime.strftime('%I').

Fix

Add an hour12 property (hour % 12, with 0 mapped to 12) and point %I
and %-I at it. A plain date (no time component) still resolves to the '12'
fallback, so existing date behavior is unchanged.

Tests

test_strftime_I_directive_uses_twelve_hour_clock sweeps all 24 hours and
asserts %I / %-I match the stdlib output, checks a %p+%I round-trip
invariant (reconstructing the 24-hour value for every hour), spot-checks the
values the old behavior got wrong (0'12', 13'01', 23'11'), and
the date-midnight→'12' fallback. Full suite: 127 passed, 3 skipped, 76
subtests. ruff check / ruff format --check clean.

Comment thread jdatetime/__init__.py Outdated
return self.__time.hour

@property
def hour12(self) -> int:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @5j9! I agree

gaoflow added 2 commits July 15, 2026 09:58
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
gaoflow force-pushed the fix-strftime-I-twelve-hour branch from 055979f to a37985b Compare July 15, 2026 07:59
@gaoflow

gaoflow commented Jul 15, 2026

Copy link
Copy Markdown
Author

Done — hour12 is now _hour12 (a37985b), and the branch is rebased onto main with the changelog entry moved to a new Unreleased section. All tests pass.

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.

3 participants