Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed

* `%I` and `%-I` now render the 12-hour clock instead of duplicating `%H`
* pass Gregorian to tzinfo.dst #188
* typing: use int for fold #186

Expand Down
10 changes: 8 additions & 2 deletions jdatetime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
'%f': ('_strftime_get_attr_value', {'attr': 'microsecond', 'fmt': '%06.d', 'fb': '000000'}),
'%H': ('_strftime_get_attr_value', {'attr': 'hour', 'fmt': '%02.d', 'fb': '00'}),
'%-H': ('_strftime_get_attr_value', {'attr': 'hour', 'fmt': '%d', 'fb': '0'}),
'%I': ('_strftime_get_attr_value', {'attr': 'hour', 'fmt': '%02.d', 'fb': '12'}),
'%-I': ('_strftime_get_attr_value', {'attr': 'hour', 'fmt': '%d', 'fb': '12'}),
'%I': ('_strftime_get_attr_value', {'attr': '_hour12', 'fmt': '%02.d', 'fb': '12'}),
'%-I': ('_strftime_get_attr_value', {'attr': '_hour12', 'fmt': '%d', 'fb': '12'}),
'%M': ('_strftime_get_attr_value', {'attr': 'minute', 'fmt': '%02.d', 'fb': '00'}),
'%-M': ('_strftime_get_attr_value', {'attr': 'minute', 'fmt': '%d', 'fb': '0'}),
'%S': ('_strftime_get_attr_value', {'attr': 'second', 'fmt': '%02.d', 'fb': '00'}),
Expand Down Expand Up @@ -871,6 +871,12 @@ def fromordinal(ordinal: int) -> datetime:
def hour(self) -> int:
return self.__time.hour

@property
def _hour12(self) -> int:
"""Return the hour on a 12-hour clock (1..12), used by the %I directive."""
hour = self.hour % 12
return 12 if hour == 0 else hour

@property
def minute(self) -> int:
return self.__time.minute
Expand Down
25 changes: 25 additions & 0 deletions tests/test_jdatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,31 @@ def dst(self, dt):
dt = jdatetime.datetime(1389, 2, 17, 19, 10, 2, tzinfo=teh)
self.assertEqual(dt.strftime('%Z %z'), 'IRDT +0330')

def test_strftime_I_directive_uses_twelve_hour_clock(self):
import datetime as _std

# %I and %-I must render the 12-hour clock (1..12), matching stdlib.
for hour in range(24):
with self.subTest(hour=hour):
jd = jdatetime.datetime(1400, 1, 1, hour, 5, 0)
std = _std.datetime(2020, 1, 1, hour, 5, 0)
self.assertEqual(jd.strftime('%I'), std.strftime('%I'))
self.assertEqual(jd.strftime('%-I'), std.strftime('%-I'))

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.

%-I (a GNU extension for %I) is not supported by the standard datetime module on Windows, which causes the std.strftime('%-I') comparison to fail in Windows test runs.

self.assertEqual(jd._hour12, int(std.strftime('%I')))
# %I must be consistent with the (correct) %p directive:
# reconstructing the 24-hour value from (%p, %I) round-trips.
twelve = int(jd.strftime('%I'))
base = 0 if twelve == 12 else twelve
reconstructed = base + (12 if jd.strftime('%p') == 'PM' else 0)
self.assertEqual(reconstructed, hour)
# Spot-check the values that the previous 24-hour behaviour got wrong.
self.assertEqual(jdatetime.datetime(1400, 1, 1, 0, 0).strftime('%I'), '12')
self.assertEqual(jdatetime.datetime(1400, 1, 1, 13, 0).strftime('%I'), '01')
self.assertEqual(jdatetime.datetime(1400, 1, 1, 23, 0).strftime('%I'), '11')
self.assertEqual(jdatetime.datetime(1400, 1, 1, 13, 0).strftime('%-I'), '1')
# A plain date (no time component) is midnight -> 12.
self.assertEqual(jdatetime.date(1400, 1, 1).strftime('%I'), '12')

def test_strftime_fa_locale_uses_short_month_names_for_b_directive(self):
tests = [
(1, 'فرو', 'فروردین'),
Expand Down
Loading