diff --git a/Lib/calendar.py b/Lib/calendar.py index 657396439c91fcb..37c2f53a3ab5482 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -7,6 +7,7 @@ import sys import datetime +from enum import IntEnum import locale as _locale from itertools import repeat @@ -16,7 +17,10 @@ "timegm", "month_name", "month_abbr", "day_name", "day_abbr", "Calendar", "TextCalendar", "HTMLCalendar", "LocaleTextCalendar", "LocaleHTMLCalendar", "weekheader", - "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", + "Weekdays","Months", "JANUARY", "FEBRUARY", "MARCH", + "APRIL", "MAY", "JUNE", "JULY", + "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMEMBER", "DECEMBER", + "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"] # Exception raised for bad input (with string parameter for details) @@ -38,8 +42,34 @@ def __str__(self): # Constants for months referenced later -January = 1 -February = 2 +class Months(IntEnum): + JANUARY = 1 + FEBRUARY = 2 + MARCH = 3 + APRIL = 4 + MAY = 5 + JUNE = 6 + JULY = 7 + AUGUST = 8 + SEPTEMBER = 9 + OCTOBER = 10 + NOVEMEMBER = 11 + DECEMBER = 12 + +(JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMEMBER, DECEMBER) = [month.value for month in Months] + +# Constants for weekdays +class Weekdays(IntEnum): + MONDAY = 0 + TUESDAY = 1 + WEDNESDAY = 2 + THURSDAY = 3 + FRIDAY = 4 + SATURDAY = 5 + SUNDAY = 6 + +# Constants for weekdays +(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = [week.value for week in Weekdays] # Number of days per month (except for February in leap years) mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] @@ -95,9 +125,6 @@ def __len__(self): month_name = _localized_month('%B') month_abbr = _localized_month('%b') -# Constants for weekdays -(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7) - def isleap(year): """Return True for leap years, False for non-leap years.""" @@ -125,12 +152,12 @@ def monthrange(year, month): if not 1 <= month <= 12: raise IllegalMonthError(month) day1 = weekday(year, month, 1) - ndays = mdays[month] + (month == February and isleap(year)) + ndays = mdays[month] + (month == FEBRUARY and isleap(year)) return day1, ndays def _monthlen(year, month): - return mdays[month] + (month == February and isleap(year)) + return mdays[month] + (month == FEBRUARY and isleap(year)) def _prevmonth(year, month): @@ -262,7 +289,7 @@ def yeardatescalendar(self, year, width=3): """ months = [ self.monthdatescalendar(year, i) - for i in range(January, January+12) + for i in range(JANUARY, JANUARY+12) ] return [months[i:i+width] for i in range(0, len(months), width) ] @@ -275,7 +302,7 @@ def yeardays2calendar(self, year, width=3): """ months = [ self.monthdays2calendar(year, i) - for i in range(January, January+12) + for i in range(JANUARY,JANUARY+12) ] return [months[i:i+width] for i in range(0, len(months), width) ] @@ -287,7 +314,7 @@ def yeardayscalendar(self, year, width=3): """ months = [ self.monthdayscalendar(year, i) - for i in range(January, January+12) + for i in range(JANUARY, JANUARY+12) ] return [months[i:i+width] for i in range(0, len(months), width) ] @@ -509,7 +536,7 @@ def formatyear(self, theyear, width=3): a('\n') a('%s' % ( width, self.cssclass_year_head, theyear)) - for i in range(January, January+12, width): + for i in range(JANUARY, JANUARY+12, width): # months in this row months = range(i, min(i+width, 13)) a('')