Skip to content

Commit b7a55ee

Browse files
committed
gh-131146: Fix month names in a genitive case in calendar module.
The calendar module displays month names in some locales using the genitive case. This is grammatically incorrect, as the nominative case should be used when the month is named by itself. To address this issue, this change introduces new lists `alt_month_name` and `alt_month_abbr` that contain month names in the nominative case. The module now uses `%OB` format specifier to get month names in the nominative case where available.
1 parent 1fb7e2a commit b7a55ee

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

Lib/calendar.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,24 @@ def __len__(self):
139139
month_name = _localized_month('%B')
140140
month_abbr = _localized_month('%b')
141141

142+
# Check if the platform supports the %OB format code
143+
def _is_alt_mon_available():
144+
try:
145+
datetime.date(2001, 1, 1).strftime('%OB')
146+
datetime.date(2001, 1, 1).strftime('%Ob')
147+
except ValueError:
148+
return False
149+
return True
150+
151+
# In Greek and in many Slavic and Baltic languages, "%OB" will produce
152+
# the month in nominative case.
153+
if _is_alt_mon_available():
154+
alt_month_name = _localized_month('%OB')
155+
alt_month_abbr = _localized_month('%Ob')
156+
else:
157+
alt_month_name = month_name
158+
alt_month_abbr = month_abbr
159+
142160

143161
def isleap(year):
144162
"""Return True for leap years, False for non-leap years."""
@@ -377,7 +395,7 @@ def formatmonthname(self, theyear, themonth, width, withyear=True):
377395
"""
378396
_validate_month(themonth)
379397

380-
s = month_name[themonth]
398+
s = alt_month_name[themonth]
381399
if withyear:
382400
s = "%s %r" % (s, theyear)
383401
return s.center(width)
@@ -510,9 +528,9 @@ def formatmonthname(self, theyear, themonth, withyear=True):
510528
"""
511529
_validate_month(themonth)
512530
if withyear:
513-
s = '%s %s' % (month_name[themonth], theyear)
531+
s = '%s %s' % (alt_month_name[themonth], theyear)
514532
else:
515-
s = '%s' % month_name[themonth]
533+
s = '%s' % alt_month_name[themonth]
516534
return '<tr><th colspan="7" class="%s">%s</th></tr>' % (
517535
self.cssclass_month_head, s)
518536

Lib/test/test_calendar.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,8 @@ def test__all__(self):
11261126
not_exported = {
11271127
'mdays', 'January', 'February', 'EPOCH',
11281128
'different_locale', 'c', 'prweek', 'week', 'format',
1129-
'formatstring', 'main', 'monthlen', 'prevmonth', 'nextmonth', ""}
1129+
'formatstring', 'main', 'monthlen', 'prevmonth', 'nextmonth',
1130+
'alt_month_name', 'alt_month_abbr', ""}
11301131
support.check__all__(self, calendar, not_exported=not_exported)
11311132

11321133

0 commit comments

Comments
 (0)