Skip to content

bpo-46266: Improve day constants (MONDAY ... SUNDAY) in calendar #30412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions Doc/library/calendar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is
2 BC, and so on.


.. class:: Calendar(firstweekday=0)
.. class:: Calendar(firstweekday=MONDAY)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave this as 0.


Creates a :class:`Calendar` object. *firstweekday* is an integer specifying the
first day of the week. ``0`` is Monday (the default), ``6`` is Sunday.
first day of the week. :const:`MONDAY` is ``0`` (the default), :const:`SUNDAY` is ``6``.

A :class:`Calendar` object provides several methods that can be used for
preparing the calendar data for formatting. This class doesn't do any formatting
Expand Down Expand Up @@ -275,13 +275,13 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is
cssclass_year = "text-italic lead"


.. class:: LocaleTextCalendar(firstweekday=0, locale=None)
.. class:: LocaleTextCalendar(firstweekday=MONDAY, locale=None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave all the defaults as 0.


This subclass of :class:`TextCalendar` can be passed a locale name in the
constructor and will return month and weekday names in the specified locale.


.. class:: LocaleHTMLCalendar(firstweekday=0, locale=None)
.. class:: LocaleHTMLCalendar(firstweekday=MONDAY, locale=None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this one.


This subclass of :class:`HTMLCalendar` can be passed a locale name in the
constructor and will return month and weekday names in the specified
Expand Down Expand Up @@ -406,6 +406,15 @@ The :mod:`calendar` module exports the following data attributes:
locale. This follows normal convention of January being month number 1, so it
has a length of 13 and ``month_abbr[0]`` is the empty string.

.. data:: MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY

Aliases for day numbers, where ``MONDAY`` is ``0`` and ``SUNDAY`` is ``6``.

.. seealso::

Expand Down
10 changes: 6 additions & 4 deletions Lib/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"monthcalendar", "prmonth", "month", "prcal", "calendar",
"timegm", "month_name", "month_abbr", "day_name", "day_abbr",
"Calendar", "TextCalendar", "HTMLCalendar", "LocaleTextCalendar",
"LocaleHTMLCalendar", "weekheader"]
"LocaleHTMLCalendar", "weekheader",
"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY",
"SATURDAY", "SUNDAY"]

# Exception raised for bad input (with string parameter for details)
error = ValueError
Expand Down Expand Up @@ -151,7 +153,7 @@ class Calendar(object):
provides data to subclasses.
"""

def __init__(self, firstweekday=0):
def __init__(self, firstweekday=MONDAY):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave all of these as 0 as well.

self.firstweekday = firstweekday # 0 = Monday, 6 = Sunday

def getfirstweekday(self):
Expand Down Expand Up @@ -561,7 +563,7 @@ class LocaleTextCalendar(TextCalendar):
month and weekday names in the specified locale.
"""

def __init__(self, firstweekday=0, locale=None):
def __init__(self, firstweekday=MONDAY, locale=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too.

TextCalendar.__init__(self, firstweekday)
if locale is None:
locale = _locale.getdefaultlocale()
Expand All @@ -581,7 +583,7 @@ class LocaleHTMLCalendar(HTMLCalendar):
This class can be passed a locale name in the constructor and will return
month and weekday names in the specified locale.
"""
def __init__(self, firstweekday=0, locale=None):
def __init__(self, firstweekday=MONDAY, locale=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too.

HTMLCalendar.__init__(self, firstweekday)
if locale is None:
locale = _locale.getdefaultlocale()
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,8 +935,7 @@ def test_html_output_year_css(self):
class MiscTestCase(unittest.TestCase):
def test__all__(self):
not_exported = {
'mdays', 'January', 'February', 'EPOCH', 'MONDAY', 'TUESDAY',
'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY',
'mdays', 'January', 'February', 'EPOCH',
'different_locale', 'c', 'prweek', 'week', 'format',
'formatstring', 'main', 'monthlen', 'prevmonth', 'nextmonth'}
support.check__all__(self, calendar, not_exported=not_exported)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Improve day constants in :mod:`calendar`.

Now all constants (`MONDAY` ... `SUNDAY`) are documented, tested, and added
to ``__all__``.