| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  | """Calendar printing functions
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Note when comparing these calendars to the ones printed by cal(1): By | 
					
						
							|  |  |  | default, these calendars have Monday as the first day of the week, and | 
					
						
							|  |  |  | Sunday as the last (the European convention). Use setfirstweekday() to | 
					
						
							|  |  |  | set the first day of the week (0=Monday, 6=Sunday)."""
 | 
					
						
							| 
									
										
										
										
											1990-10-13 19:23:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-25 16:37:19 +00:00
										 |  |  | import datetime | 
					
						
							| 
									
										
										
										
											1990-10-13 19:23:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-20 19:54:20 +00:00
										 |  |  | __all__ = ["error","setfirstweekday","firstweekday","isleap", | 
					
						
							|  |  |  |            "leapdays","weekday","monthrange","monthcalendar", | 
					
						
							| 
									
										
										
										
											2002-03-23 03:26:53 +00:00
										 |  |  |            "prmonth","month","prcal","calendar","timegm", | 
					
						
							| 
									
										
										
										
											2005-05-10 03:20:12 +00:00
										 |  |  |            "month_name", "month_abbr", "day_name", "day_abbr", | 
					
						
							|  |  |  |            "weekheader"] | 
					
						
							| 
									
										
										
										
											2001-01-20 19:54:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-06-20 21:02:22 +00:00
										 |  |  | # Exception raised for bad input (with string parameter for details) | 
					
						
							| 
									
										
										
										
											1999-05-03 18:07:40 +00:00
										 |  |  | error = ValueError | 
					
						
							| 
									
										
										
										
											1993-06-17 12:38:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-10-13 19:23:40 +00:00
										 |  |  | # Constants for months referenced later | 
					
						
							|  |  |  | January = 1 | 
					
						
							|  |  |  | February = 2 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Number of days per month (except for February in leap years) | 
					
						
							| 
									
										
										
										
											1992-07-09 11:05:12 +00:00
										 |  |  | mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | 
					
						
							| 
									
										
										
										
											1990-10-13 19:23:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-23 03:26:53 +00:00
										 |  |  | # This module used to have hard-coded lists of day and month names, as | 
					
						
							|  |  |  | # English strings.  The classes following emulate a read-only version of | 
					
						
							|  |  |  | # that, but supply localized names.  Note that the values are computed | 
					
						
							|  |  |  | # fresh on each call, in case the user changes locale between calls. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-20 03:38:12 +00:00
										 |  |  | class _localized_month: | 
					
						
							| 
									
										
										
										
											2004-11-13 16:18:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     _months = [datetime.date(2001, i+1, 1).strftime for i in range(12)] | 
					
						
							|  |  |  |     _months.insert(0, lambda x: "") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-23 03:26:53 +00:00
										 |  |  |     def __init__(self, format): | 
					
						
							| 
									
										
										
										
											2001-05-22 15:58:30 +00:00
										 |  |  |         self.format = format | 
					
						
							| 
									
										
										
										
											2002-03-23 03:26:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __getitem__(self, i): | 
					
						
							| 
									
										
										
										
											2004-11-13 16:18:32 +00:00
										 |  |  |         funcs = self._months[i] | 
					
						
							|  |  |  |         if isinstance(i, slice): | 
					
						
							|  |  |  |             return [f(self.format) for f in funcs] | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return funcs(self.format) | 
					
						
							| 
									
										
										
										
											2002-03-23 03:26:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-15 04:08:38 +00:00
										 |  |  |     def __len__(self): | 
					
						
							| 
									
										
										
										
											2002-03-23 03:26:53 +00:00
										 |  |  |         return 13 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-20 03:38:12 +00:00
										 |  |  | class _localized_day: | 
					
						
							| 
									
										
										
										
											2004-11-13 16:18:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # January 1, 2001, was a Monday. | 
					
						
							|  |  |  |     _days = [datetime.date(2001, 1, i+1).strftime for i in range(7)] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-23 03:26:53 +00:00
										 |  |  |     def __init__(self, format): | 
					
						
							|  |  |  |         self.format = format | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __getitem__(self, i): | 
					
						
							| 
									
										
										
										
											2004-11-13 16:18:32 +00:00
										 |  |  |         funcs = self._days[i] | 
					
						
							|  |  |  |         if isinstance(i, slice): | 
					
						
							|  |  |  |             return [f(self.format) for f in funcs] | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return funcs(self.format) | 
					
						
							| 
									
										
										
										
											2002-03-23 03:26:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-07 03:47:06 +00:00
										 |  |  |     def __len__(self): | 
					
						
							| 
									
										
										
										
											2002-03-23 03:26:53 +00:00
										 |  |  |         return 7 | 
					
						
							| 
									
										
										
										
											2001-05-22 15:58:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-10-13 19:23:40 +00:00
										 |  |  | # Full and abbreviated names of weekdays | 
					
						
							| 
									
										
										
										
											2002-03-23 03:26:53 +00:00
										 |  |  | day_name = _localized_day('%A') | 
					
						
							|  |  |  | day_abbr = _localized_day('%a') | 
					
						
							| 
									
										
										
										
											1990-10-13 19:23:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-06-23 09:30:50 +00:00
										 |  |  | # Full and abbreviated names of months (1-based arrays!!!) | 
					
						
							| 
									
										
										
										
											2002-03-23 03:26:53 +00:00
										 |  |  | month_name = _localized_month('%B') | 
					
						
							|  |  |  | month_abbr = _localized_month('%b') | 
					
						
							| 
									
										
										
										
											1990-10-13 19:23:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  | # Constants for weekdays | 
					
						
							|  |  |  | (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _firstweekday = 0                       # 0 = Monday, 6 = Sunday | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def firstweekday(): | 
					
						
							|  |  |  |     return _firstweekday | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def setfirstweekday(weekday): | 
					
						
							|  |  |  |     """Set weekday (Monday=0, Sunday=6) to start each week.""" | 
					
						
							|  |  |  |     global _firstweekday | 
					
						
							|  |  |  |     if not MONDAY <= weekday <= SUNDAY: | 
					
						
							|  |  |  |         raise ValueError, \ | 
					
						
							|  |  |  |               'bad weekday number; must be 0 (Monday) to 6 (Sunday)' | 
					
						
							|  |  |  |     _firstweekday = weekday | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-06-20 21:02:22 +00:00
										 |  |  | def isleap(year): | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |     """Return 1 for leap years, 0 for non-leap years.""" | 
					
						
							| 
									
										
										
										
											2000-12-12 23:20:45 +00:00
										 |  |  |     return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) | 
					
						
							| 
									
										
										
										
											1990-10-13 19:23:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-06-20 21:02:22 +00:00
										 |  |  | def leapdays(y1, y2): | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |     """Return number of leap years in range [y1, y2).
 | 
					
						
							| 
									
										
										
										
											2000-10-09 12:42:04 +00:00
										 |  |  |        Assume y1 <= y2."""
 | 
					
						
							|  |  |  |     y1 -= 1 | 
					
						
							|  |  |  |     y2 -= 1 | 
					
						
							| 
									
										
										
										
											2002-12-25 16:37:19 +00:00
										 |  |  |     return (y2//4 - y1//4) - (y2//100 - y1//100) + (y2//400 - y1//400) | 
					
						
							| 
									
										
										
										
											1990-10-13 19:23:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def weekday(year, month, day): | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |     """Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12),
 | 
					
						
							|  |  |  |        day (1-31)."""
 | 
					
						
							| 
									
										
										
										
											2002-12-25 16:37:19 +00:00
										 |  |  |     return datetime.date(year, month, day).weekday() | 
					
						
							| 
									
										
										
										
											1990-10-13 19:23:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def monthrange(year, month): | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |     """Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for
 | 
					
						
							|  |  |  |        year, month."""
 | 
					
						
							|  |  |  |     if not 1 <= month <= 12: | 
					
						
							|  |  |  |         raise ValueError, 'bad month number' | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |     day1 = weekday(year, month, 1) | 
					
						
							|  |  |  |     ndays = mdays[month] + (month == February and isleap(year)) | 
					
						
							|  |  |  |     return day1, ndays | 
					
						
							| 
									
										
										
										
											1990-10-13 19:23:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  | def monthcalendar(year, month): | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |     """Return a matrix representing a month's calendar.
 | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |        Each row represents a week; days outside this month are zero."""
 | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |     day1, ndays = monthrange(year, month) | 
					
						
							|  |  |  |     rows = [] | 
					
						
							|  |  |  |     r7 = range(7) | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |     day = (_firstweekday - day1 + 6) % 7 - 5   # for leading 0's in first week | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |     while day <= ndays: | 
					
						
							|  |  |  |         row = [0, 0, 0, 0, 0, 0, 0] | 
					
						
							|  |  |  |         for i in r7: | 
					
						
							|  |  |  |             if 1 <= day <= ndays: row[i] = day | 
					
						
							|  |  |  |             day = day + 1 | 
					
						
							|  |  |  |         rows.append(row) | 
					
						
							|  |  |  |     return rows | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  | def prweek(theweek, width): | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |     """Print a single week (no newline).""" | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |     print week(theweek, width), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def week(theweek, width): | 
					
						
							|  |  |  |     """Returns a single week in a string (no newline).""" | 
					
						
							|  |  |  |     days = [] | 
					
						
							|  |  |  |     for day in theweek: | 
					
						
							|  |  |  |         if day == 0: | 
					
						
							|  |  |  |             s = '' | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             s = '%2i' % day             # right-align single-digit days | 
					
						
							| 
									
										
										
										
											2002-10-22 05:15:17 +00:00
										 |  |  |         days.append(s.center(width)) | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |     return ' '.join(days) | 
					
						
							| 
									
										
										
										
											1990-10-13 19:23:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def weekheader(width): | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |     """Return a header for a week.""" | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |     if width >= 9: | 
					
						
							|  |  |  |         names = day_name | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         names = day_abbr | 
					
						
							|  |  |  |     days = [] | 
					
						
							|  |  |  |     for i in range(_firstweekday, _firstweekday + 7): | 
					
						
							| 
									
										
										
										
											2002-10-22 05:15:17 +00:00
										 |  |  |         days.append(names[i%7][:width].center(width)) | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |     return ' '.join(days) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def prmonth(theyear, themonth, w=0, l=0): | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |     """Print a month's calendar.""" | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |     print month(theyear, themonth, w, l), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def month(theyear, themonth, w=0, l=0): | 
					
						
							|  |  |  |     """Return a month's calendar string (multi-line).""" | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |     w = max(2, w) | 
					
						
							|  |  |  |     l = max(1, l) | 
					
						
							| 
									
										
										
										
											2004-02-12 17:35:32 +00:00
										 |  |  |     s = ("%s %r" % (month_name[themonth], theyear)).center( | 
					
						
							|  |  |  |                  7 * (w + 1) - 1).rstrip() + \ | 
					
						
							|  |  |  |          '\n' * l + weekheader(w).rstrip() + '\n' * l | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |     for aweek in monthcalendar(theyear, themonth): | 
					
						
							|  |  |  |         s = s + week(aweek, w).rstrip() + '\n' * l | 
					
						
							|  |  |  |     return s[:-l] + '\n' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Spacing of month columns for 3-column year calendar | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  | _colwidth = 7*3 - 1         # Amount printed by prweek() | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  | _spacing = 6                # Number of spaces between columns | 
					
						
							| 
									
										
										
										
											1990-10-13 19:23:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  | def format3c(a, b, c, colwidth=_colwidth, spacing=_spacing): | 
					
						
							|  |  |  |     """Prints 3-column formatting for year calendars""" | 
					
						
							|  |  |  |     print format3cstring(a, b, c, colwidth, spacing) | 
					
						
							| 
									
										
										
										
											1990-10-13 19:23:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  | def format3cstring(a, b, c, colwidth=_colwidth, spacing=_spacing): | 
					
						
							|  |  |  |     """Returns a string formatted from 3 strings, centered within 3 columns.""" | 
					
						
							| 
									
										
										
										
											2002-10-22 05:15:17 +00:00
										 |  |  |     return (a.center(colwidth) + ' ' * spacing + b.center(colwidth) + | 
					
						
							|  |  |  |             ' ' * spacing + c.center(colwidth)) | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def prcal(year, w=0, l=0, c=_spacing): | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |     """Print a year's calendar.""" | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |     print calendar(year, w, l, c), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def calendar(year, w=0, l=0, c=_spacing): | 
					
						
							|  |  |  |     """Returns a year's calendar as a multi-line string.""" | 
					
						
							|  |  |  |     w = max(2, w) | 
					
						
							|  |  |  |     l = max(1, l) | 
					
						
							|  |  |  |     c = max(2, c) | 
					
						
							|  |  |  |     colwidth = (w + 1) * 7 - 1 | 
					
						
							| 
									
										
										
										
											2004-02-12 17:35:32 +00:00
										 |  |  |     s = repr(year).center(colwidth * 3 + c * 2).rstrip() + '\n' * l | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |     header = weekheader(w) | 
					
						
							|  |  |  |     header = format3cstring(header, header, header, colwidth, c).rstrip() | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |     for q in range(January, January+12, 3): | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |         s = (s + '\n' * l + | 
					
						
							|  |  |  |              format3cstring(month_name[q], month_name[q+1], month_name[q+2], | 
					
						
							| 
									
										
										
										
											2001-01-14 23:36:06 +00:00
										 |  |  |                             colwidth, c).rstrip() + | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |              '\n' * l + header + '\n' * l) | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |         data = [] | 
					
						
							|  |  |  |         height = 0 | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |         for amonth in range(q, q + 3): | 
					
						
							|  |  |  |             cal = monthcalendar(year, amonth) | 
					
						
							|  |  |  |             if len(cal) > height: | 
					
						
							|  |  |  |                 height = len(cal) | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |             data.append(cal) | 
					
						
							|  |  |  |         for i in range(height): | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |             weeks = [] | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |             for cal in data: | 
					
						
							|  |  |  |                 if i >= len(cal): | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |                     weeks.append('') | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |                 else: | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |                     weeks.append(week(cal[i], w)) | 
					
						
							| 
									
										
										
										
											2001-01-14 23:36:06 +00:00
										 |  |  |             s = s + format3cstring(weeks[0], weeks[1], weeks[2], | 
					
						
							| 
									
										
										
										
											2000-08-30 14:01:28 +00:00
										 |  |  |                                    colwidth, c).rstrip() + '\n' * l | 
					
						
							|  |  |  |     return s[:-l] + '\n' | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-06-09 15:07:38 +00:00
										 |  |  | EPOCH = 1970 | 
					
						
							| 
									
										
										
										
											2002-12-25 16:37:19 +00:00
										 |  |  | _EPOCH_ORD = datetime.date(EPOCH, 1, 1).toordinal() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-06-09 15:07:38 +00:00
										 |  |  | def timegm(tuple): | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |     """Unrelated but handy function to calculate Unix timestamp from GMT.""" | 
					
						
							|  |  |  |     year, month, day, hour, minute, second = tuple[:6] | 
					
						
							| 
									
										
										
										
											2003-02-13 22:58:02 +00:00
										 |  |  |     days = datetime.date(year, month, 1).toordinal() - _EPOCH_ORD + day - 1 | 
					
						
							| 
									
										
										
										
											2000-02-02 15:10:15 +00:00
										 |  |  |     hours = days*24 + hour | 
					
						
							|  |  |  |     minutes = hours*60 + minute | 
					
						
							|  |  |  |     seconds = minutes*60 + second | 
					
						
							|  |  |  |     return seconds |