Skip to content

Commit 3143cee

Browse files
authored
gh-137481: Fix abbreviation of day names in TextCalendar (GH-137482)
Use the length of the longest day name in the current locale, rather than a constant 9, to decide if the names should be abbreviated.
1 parent 7fda8b6 commit 3143cee

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

Lib/calendar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ def formatweekday(self, day, width):
378378
"""
379379
Returns a formatted week day name.
380380
"""
381-
if width >= 9:
381+
if width >= max(map(len, day_name)):
382382
names = day_name
383383
else:
384384
names = day_abbr

Lib/test/test_calendar.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,52 @@ def test_locale_calendar_formatweekday(self):
696696
except locale.Error:
697697
raise unittest.SkipTest('cannot set the en_US locale')
698698

699+
# These locales have weekday names all shorter than English's longest
700+
# 'Wednesday'. They should not be abbreviated unnecessarily
701+
@support.run_with_locales("LC_ALL",
702+
'Chinese', 'zh_CN.UTF-8',
703+
'French', 'fr_FR.UTF-8',
704+
'Norwegian', 'nb_NO.UTF-8',
705+
'Malay', 'ms_MY.UTF8'
706+
)
707+
def test_locale_calendar_short_weekday_names(self):
708+
names = (datetime.date(2001, 1, i+1).strftime('%A') for i in range(7))
709+
max_length = max(map(len, names))
710+
if max_length >= 9:
711+
self.skipTest('weekday names are too long')
712+
713+
def get_weekday_names(width):
714+
return calendar.TextCalendar().formatweekheader(width).split()
715+
716+
# Weekday names should not be abbreviated if the width is sufficient
717+
self.assertEqual(
718+
get_weekday_names(max_length),
719+
get_weekday_names(max_length + 10)
720+
)
721+
722+
# Any width shorter than necessary should produce abbreviations
723+
self.assertNotEqual(
724+
get_weekday_names(max_length),
725+
get_weekday_names(max_length - 1)
726+
)
727+
728+
# These locales have a weekday name longer than 'Wednesday'
729+
# They should be properly abbreviated rather than truncated
730+
@support.run_with_locales("LC_ALL",
731+
'Portuguese', 'pt_PT.UTF-8',
732+
'German', 'de_DE.UTF-8',
733+
'Russian', 'ru_RU.UTF-8',
734+
)
735+
def test_locale_calendar_long_weekday_names(self):
736+
names = (datetime.date(2001, 1, i+1).strftime('%A') for i in range(7))
737+
max_length = max(map(len, names))
738+
if max_length <= 9:
739+
self.skipTest('weekday names are too short')
740+
741+
def get_weekday_names(width):
742+
return calendar.TextCalendar().formatweekheader(width).split()
743+
self.assertEqual(get_weekday_names(4), get_weekday_names(9))
744+
699745
def test_locale_calendar_formatmonthname(self):
700746
try:
701747
# formatmonthname uses the same month names regardless of the width argument.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Calendar uses the lengths of the locale's weekdays to decide if the width
2+
requires abbreviation.

0 commit comments

Comments
 (0)