summaryrefslogtreecommitdiffstats
path: root/Lib/calendar.py
diff options
context:
space:
mode:
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>2016-09-28 00:27:55 (GMT)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>2016-09-28 00:27:55 (GMT)
commite3fd248c771e1a8bf337a4a307dacf1e85106611 (patch)
tree3991349a04581590bc8d6099520554cbaa02acb6 /Lib/calendar.py
parent6703e042fa0005dbf76578ddf24c7c170a715061 (diff)
parent957b75699fd1ed2eb18e8f7fa6fee1062fabad68 (diff)
downloadcpython-e3fd248c771e1a8bf337a4a307dacf1e85106611.zip
cpython-e3fd248c771e1a8bf337a4a307dacf1e85106611.tar.gz
cpython-e3fd248c771e1a8bf337a4a307dacf1e85106611.tar.bz2
Issue #28253: Fixed calendar functions for extreme months: 0001-01 and 9999-12.
Methods itermonthdays() and itermonthdays2() are reimplemented so that they don't call itermonthdates() which can cause datetime.date under/overflow.
Diffstat (limited to 'Lib/calendar.py')
-rw-r--r--Lib/calendar.py19
1 files changed, 9 insertions, 10 deletions
diff --git a/Lib/calendar.py b/Lib/calendar.py
index 85baf2e..92149b7 100644
--- a/Lib/calendar.py
+++ b/Lib/calendar.py
@@ -8,6 +8,7 @@ set the first day of the week (0=Monday, 6=Sunday)."""
import sys
import datetime
import locale as _locale
+from itertools import repeat
__all__ = ["IllegalMonthError", "IllegalWeekdayError", "setfirstweekday",
"firstweekday", "isleap", "leapdays", "weekday", "monthrange",
@@ -176,22 +177,20 @@ class Calendar(object):
Like itermonthdates(), but will yield (day number, weekday number)
tuples. For days outside the specified month the day number is 0.
"""
- for date in self.itermonthdates(year, month):
- if date.month != month:
- yield (0, date.weekday())
- else:
- yield (date.day, date.weekday())
+ for i, d in enumerate(self.itermonthdays(year, month), self.firstweekday):
+ yield d, i % 7
def itermonthdays(self, year, month):
"""
Like itermonthdates(), but will yield day numbers. For days outside
the specified month the day number is 0.
"""
- for date in self.itermonthdates(year, month):
- if date.month != month:
- yield 0
- else:
- yield date.day
+ day1, ndays = monthrange(year, month)
+ days_before = (day1 - self.firstweekday) % 7
+ yield from repeat(0, days_before)
+ yield from range(1, ndays + 1)
+ days_after = (self.firstweekday - day1 - ndays) % 7
+ yield from repeat(0, days_after)
def monthdatescalendar(self, year, month):
"""