Skip to content

Commit fb1c5ee

Browse files
committed
Make alt_month_name and alt_month_abbr public in the calendar module.
Expose alternative month names/abbreviations so users can benefit from alternative forms when they are available. Update documentation and tests accordingly.
1 parent b7a55ee commit fb1c5ee

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

Doc/library/calendar.rst

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,10 @@ The :mod:`calendar` module exports the following data attributes:
493493

494494
.. data:: month_name
495495

496-
A sequence that represents the months of the year in the current locale. This
497-
follows normal convention of January being month number 1, so it has a length of
498-
13 and ``month_name[0]`` is the empty string.
496+
A sequence that represents the months of the year in the current locale
497+
in the grammatical form used when the month is part of a complete date.
498+
This follows normal convention of January being month number 1, so it has
499+
a length of 13 and ``month_name[0]`` is the empty string.
499500

500501
>>> import calendar
501502
>>> list(calendar.month_name)
@@ -505,13 +506,39 @@ The :mod:`calendar` module exports the following data attributes:
505506
.. data:: month_abbr
506507

507508
A sequence that represents the abbreviated months of the year in the current
508-
locale. This follows normal convention of January being month number 1, so it
509-
has a length of 13 and ``month_abbr[0]`` is the empty string.
509+
locale in the grammatical form used when the month is part of a complete date.
510+
This follows normal convention of January being month number 1, so it has
511+
a length of 13 and ``month_abbr[0]`` is the empty string.
510512

511513
>>> import calendar
512514
>>> list(calendar.month_abbr)
513515
['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
514516

517+
518+
.. data:: alt_month_name
519+
520+
A sequence that represents the months of the year in the current locale
521+
in the grammatical form used when the month is named by itself if the locale
522+
provides one. If the locale does not supply an alternative form, it falls back
523+
to the behavior of :data:`month_name`.
524+
525+
>>> import calendar
526+
>>> list(calendar.alt_month_name)
527+
['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
528+
529+
530+
.. data:: alt_month_abbr
531+
532+
A sequence that represents the abbreviated months of the year in the current
533+
locale in the grammatical form used when the month is named by itself if the
534+
locale provides one. If the locale does not supply an alternative form, it falls
535+
back to the behavior of :data:`month_abbr`.
536+
537+
>>> import calendar
538+
>>> list(calendar.alt_month_abbr)
539+
['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
540+
541+
515542
.. data:: JANUARY
516543
FEBRUARY
517544
MARCH

Lib/calendar.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
__all__ = ["IllegalMonthError", "IllegalWeekdayError", "setfirstweekday",
1515
"firstweekday", "isleap", "leapdays", "weekday", "monthrange",
1616
"monthcalendar", "prmonth", "month", "prcal", "calendar",
17-
"timegm", "month_name", "month_abbr", "day_name", "day_abbr",
18-
"Calendar", "TextCalendar", "HTMLCalendar", "LocaleTextCalendar",
17+
"timegm", "month_name", "month_abbr", "alt_month_name",
18+
"alt_month_abbr", "day_name", "day_abbr", "Calendar",
19+
"TextCalendar", "HTMLCalendar", "LocaleTextCalendar",
1920
"LocaleHTMLCalendar", "weekheader",
2021
"Day", "Month", "JANUARY", "FEBRUARY", "MARCH",
2122
"APRIL", "MAY", "JUNE", "JULY",
@@ -139,7 +140,7 @@ def __len__(self):
139140
month_name = _localized_month('%B')
140141
month_abbr = _localized_month('%b')
141142

142-
# Check if the platform supports the %OB format code
143+
# Check if the platform supports %OB and %Ob specifiers
143144
def _is_alt_mon_available():
144145
try:
145146
datetime.date(2001, 1, 1).strftime('%OB')
@@ -148,8 +149,9 @@ def _is_alt_mon_available():
148149
return False
149150
return True
150151

151-
# In Greek and in many Slavic and Baltic languages, "%OB" will produce
152-
# the month in nominative case.
152+
# On platforms that support the %OB and %Ob specifiers, it is used to get the
153+
# standalone form of the month name. This is required for some languages
154+
# such as Greek, Slavic, and Baltic languages.
153155
if _is_alt_mon_available():
154156
alt_month_name = _localized_month('%OB')
155157
alt_month_abbr = _localized_month('%Ob')

Lib/test/test_calendar.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,8 @@ def test_days(self):
546546
self.assertEqual(value[::-1], list(reversed(value)))
547547

548548
def test_months(self):
549-
for attr in "month_name", "month_abbr":
549+
for attr in ["month_name", "month_abbr", "alt_month_name",
550+
"alt_month_abbr"]:
550551
value = getattr(calendar, attr)
551552
self.assertEqual(len(value), 13)
552553
self.assertEqual(len(value[:]), 13)
@@ -556,6 +557,13 @@ def test_months(self):
556557
# verify it "acts like a sequence" in two forms of iteration
557558
self.assertEqual(value[::-1], list(reversed(value)))
558559

560+
def test_alt_month_name_and_abbr(self):
561+
# Ensure that the alternate month names and abbreviations are equal
562+
# to the regular month names and abbreviations for the "C" locale.
563+
with calendar.different_locale("C"):
564+
self.assertEqual(list(calendar.month_name), list(calendar.alt_month_name))
565+
self.assertEqual(list(calendar.month_abbr), list(calendar.alt_month_abbr))
566+
559567
def test_locale_text_calendar(self):
560568
try:
561569
cal = calendar.LocaleTextCalendar(locale='')
@@ -1126,8 +1134,7 @@ def test__all__(self):
11261134
not_exported = {
11271135
'mdays', 'January', 'February', 'EPOCH',
11281136
'different_locale', 'c', 'prweek', 'week', 'format',
1129-
'formatstring', 'main', 'monthlen', 'prevmonth', 'nextmonth',
1130-
'alt_month_name', 'alt_month_abbr', ""}
1137+
'formatstring', 'main', 'monthlen', 'prevmonth', 'nextmonth', ""}
11311138
support.check__all__(self, calendar, not_exported=not_exported)
11321139

11331140

0 commit comments

Comments
 (0)