Skip to content
2 changes: 1 addition & 1 deletion Lib/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def formatweekday(self, day, width):
"""
Returns a formatted week day name.
"""
if width >= 9:
if width >= max(map(len, day_name)):
names = day_name
else:
names = day_abbr
Expand Down
30 changes: 30 additions & 0 deletions Lib/test/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,36 @@ def test_locale_calendar_formatweekday(self):
self.assertEqual(cal.formatweekday(0, 10), " Monday ")
except locale.Error:
raise unittest.SkipTest('cannot set the en_US locale')

# These locales have weekday names all shorter than English's longest 'Wednesday'
# They should not be abbreviated unnecessarily
@support.run_with_locale("LC_ALL",
'Chinese', 'zh_CN.UTF-8',
'French', 'fr_FR.UTF-8',
'Norwegian', 'nb_NO.UTF-8',
'Malay', 'ms_MY.UTF8'
)
def test_locale_calendar_weekday_names(self):
max_length = max(map(len, (datetime.date(2001, 1, i+1).strftime('%A') for i in range(7))))

get_weekday_names = lambda width: calendar.TextCalendar().formatweekheader(width).split()

# Full weekday name, not an abbreviation, should be used if the width is sufficient
self.assertEqual(get_weekday_names(max_length), get_weekday_names(max_length + 10))

# Any width shorter than the longest necessary should produce abbreviations
self.assertNotEqual(get_weekday_names(max_length), get_weekday_names(max_length - 1))

# These locales have a weekday name longer than English's longest 'Wednesday'
# They should be properly abbreviated rather than truncated
@support.run_with_locale("LC_ALL",
'Portuguese', 'pt_PT.UTF-8',
'German', 'de_DE.UTF-8',
'Russian', 'ru_RU.UTF-8',
)
def test_locale_calendar_long_weekday_names(self):
get_weekday_names = lambda width: calendar.TextCalendar().formatweekheader(width).split()
self.assertEqual(get_weekday_names(4), get_weekday_names(9))

def test_locale_calendar_formatmonthname(self):
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Calendar uses the lengths of the locale's weekdays to decide if the width
requires abbreviation.
Loading