Base calendar class. This class doesn't do any formatting. It simply provides data to subclasses.
| 148 | |
| 149 | |
| 150 | class Calendar(object): |
| 151 | """ |
| 152 | Base calendar class. This class doesn't do any formatting. It simply |
| 153 | provides data to subclasses. |
| 154 | """ |
| 155 | |
| 156 | def __init__(self, firstweekday=0): |
| 157 | self.firstweekday = firstweekday # 0 = Monday, 6 = Sunday |
| 158 | |
| 159 | def getfirstweekday(self): |
| 160 | return self._firstweekday % 7 |
| 161 | |
| 162 | def setfirstweekday(self, firstweekday): |
| 163 | self._firstweekday = firstweekday |
| 164 | |
| 165 | firstweekday = property(getfirstweekday, setfirstweekday) |
| 166 | |
| 167 | def iterweekdays(self): |
| 168 | """ |
| 169 | Return an iterator for one week of weekday numbers starting with the |
| 170 | configured first one. |
| 171 | """ |
| 172 | for i in range(self.firstweekday, self.firstweekday + 7): |
| 173 | yield i%7 |
| 174 | |
| 175 | def itermonthdates(self, year, month): |
| 176 | """ |
| 177 | Return an iterator for one month. The iterator will yield datetime.date |
| 178 | values and will always iterate through complete weeks, so it will yield |
| 179 | dates outside the specified month. |
| 180 | """ |
| 181 | for y, m, d in self.itermonthdays3(year, month): |
| 182 | yield datetime.date(y, m, d) |
| 183 | |
| 184 | def itermonthdays(self, year, month): |
| 185 | """ |
| 186 | Like itermonthdates(), but will yield day numbers. For days outside |
| 187 | the specified month the day number is 0. |
| 188 | """ |
| 189 | day1, ndays = monthrange(year, month) |
| 190 | days_before = (day1 - self.firstweekday) % 7 |
| 191 | yield from repeat(0, days_before) |
| 192 | yield from range(1, ndays + 1) |
| 193 | days_after = (self.firstweekday - day1 - ndays) % 7 |
| 194 | yield from repeat(0, days_after) |
| 195 | |
| 196 | def itermonthdays2(self, year, month): |
| 197 | """ |
| 198 | Like itermonthdates(), but will yield (day number, weekday number) |
| 199 | tuples. For days outside the specified month the day number is 0. |
| 200 | """ |
| 201 | for i, d in enumerate(self.itermonthdays(year, month), self.firstweekday): |
| 202 | yield d, i % 7 |
| 203 | |
| 204 | def itermonthdays3(self, year, month): |
| 205 | """ |
| 206 | Like itermonthdates(), but will yield (year, month, day) tuples. Can be |
| 207 | used for dates outside of datetime.date range. |