summaryrefslogtreecommitdiffstats
path: root/Lib/calendar.py
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2022-02-24 13:29:08 (GMT)
committerGitHub <noreply@github.com>2022-02-24 13:29:08 (GMT)
commit4fccf910738d1442852cb900747e6dccb8fe03ef (patch)
tree55d6f9d4fc4f1960cba7daa3d27a77af0c5200eb /Lib/calendar.py
parentae3adbeaedb0536292f4542a0d42063410ae150f (diff)
downloadcpython-4fccf910738d1442852cb900747e6dccb8fe03ef.zip
cpython-4fccf910738d1442852cb900747e6dccb8fe03ef.tar.gz
cpython-4fccf910738d1442852cb900747e6dccb8fe03ef.tar.bz2
bpo-46659: Enhance LocaleTextCalendar for C locale (GH-31214)
If the LC_TIME locale is "C", use the user preferred locale.
Diffstat (limited to 'Lib/calendar.py')
-rw-r--r--Lib/calendar.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/Lib/calendar.py b/Lib/calendar.py
index 361898d..6573964 100644
--- a/Lib/calendar.py
+++ b/Lib/calendar.py
@@ -548,15 +548,28 @@ class HTMLCalendar(Calendar):
class different_locale:
def __init__(self, locale):
self.locale = locale
+ self.oldlocale = None
def __enter__(self):
- self.oldlocale = _locale.getlocale(_locale.LC_TIME)
+ self.oldlocale = _locale.setlocale(_locale.LC_TIME, None)
_locale.setlocale(_locale.LC_TIME, self.locale)
def __exit__(self, *args):
+ if self.oldlocale is None:
+ return
_locale.setlocale(_locale.LC_TIME, self.oldlocale)
+def _get_default_locale():
+ locale = _locale.setlocale(_locale.LC_TIME, None)
+ if locale == "C":
+ with different_locale(""):
+ # The LC_TIME locale does not seem to be configured:
+ # get the user preferred locale.
+ locale = _locale.setlocale(_locale.LC_TIME, None)
+ return locale
+
+
class LocaleTextCalendar(TextCalendar):
"""
This class can be passed a locale name in the constructor and will return
@@ -566,7 +579,7 @@ class LocaleTextCalendar(TextCalendar):
def __init__(self, firstweekday=0, locale=None):
TextCalendar.__init__(self, firstweekday)
if locale is None:
- locale = _locale.getlocale(_locale.LC_TIME)
+ locale = _get_default_locale()
self.locale = locale
def formatweekday(self, day, width):
@@ -586,7 +599,7 @@ class LocaleHTMLCalendar(HTMLCalendar):
def __init__(self, firstweekday=0, locale=None):
HTMLCalendar.__init__(self, firstweekday)
if locale is None:
- locale = _locale.getlocale(_locale.LC_TIME)
+ locale = _get_default_locale()
self.locale = locale
def formatweekday(self, day):