Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 1 | """Calendar printing functions |
| 2 | |
| 3 | Note when comparing these calendars to the ones printed by cal(1): By |
| 4 | default, these calendars have Monday as the first day of the week, and |
| 5 | Sunday as the last (the European convention). Use setfirstweekday() to |
| 6 | set the first day of the week (0=Monday, 6=Sunday).""" |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 7 | |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 8 | # Revision 2: uses functions from built-in time module |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 10 | # Import functions and variables from time module |
Barry Warsaw | 1d09910 | 2001-05-22 15:58:30 +0000 | [diff] [blame] | 11 | from time import localtime, mktime, strftime |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 12 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 13 | __all__ = ["error","setfirstweekday","firstweekday","isleap", |
| 14 | "leapdays","weekday","monthrange","monthcalendar", |
| 15 | "prmonth","month","prcal","calendar","timegm"] |
| 16 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 17 | # Exception raised for bad input (with string parameter for details) |
Guido van Rossum | 00245cf | 1999-05-03 18:07:40 +0000 | [diff] [blame] | 18 | error = ValueError |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 20 | # Constants for months referenced later |
| 21 | January = 1 |
| 22 | February = 2 |
| 23 | |
| 24 | # Number of days per month (except for February in leap years) |
| 25 | mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] |
| 26 | |
Barry Warsaw | 1d09910 | 2001-05-22 15:58:30 +0000 | [diff] [blame] | 27 | class _localized_name: |
| 28 | def __init__(self, format): |
| 29 | self.format = format |
| 30 | def __getitem__(self, item): |
| 31 | return strftime(self.format, (item,)*9).capitalize() |
| 32 | |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 33 | # Full and abbreviated names of weekdays |
Barry Warsaw | 1d09910 | 2001-05-22 15:58:30 +0000 | [diff] [blame] | 34 | day_name = _localized_name('%A') |
| 35 | day_abbr = _localized_name('%a') |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 37 | # Full and abbreviated names of months (1-based arrays!!!) |
Barry Warsaw | 1d09910 | 2001-05-22 15:58:30 +0000 | [diff] [blame] | 38 | month_name = _localized_name('%B') |
| 39 | month_abbr = _localized_name('%b') |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 40 | |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 41 | # Constants for weekdays |
| 42 | (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7) |
| 43 | |
| 44 | _firstweekday = 0 # 0 = Monday, 6 = Sunday |
| 45 | |
| 46 | def firstweekday(): |
| 47 | return _firstweekday |
| 48 | |
| 49 | def setfirstweekday(weekday): |
| 50 | """Set weekday (Monday=0, Sunday=6) to start each week.""" |
| 51 | global _firstweekday |
| 52 | if not MONDAY <= weekday <= SUNDAY: |
| 53 | raise ValueError, \ |
| 54 | 'bad weekday number; must be 0 (Monday) to 6 (Sunday)' |
| 55 | _firstweekday = weekday |
| 56 | |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 57 | def isleap(year): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 58 | """Return 1 for leap years, 0 for non-leap years.""" |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 59 | return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 61 | def leapdays(y1, y2): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 62 | """Return number of leap years in range [y1, y2). |
Guido van Rossum | 46735ad | 2000-10-09 12:42:04 +0000 | [diff] [blame] | 63 | Assume y1 <= y2.""" |
| 64 | y1 -= 1 |
| 65 | y2 -= 1 |
| 66 | return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400) |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 68 | def weekday(year, month, day): |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 69 | """Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12), |
| 70 | day (1-31).""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 71 | secs = mktime((year, month, day, 0, 0, 0, 0, 0, 0)) |
| 72 | tuple = localtime(secs) |
| 73 | return tuple[6] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 75 | def monthrange(year, month): |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 76 | """Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for |
| 77 | year, month.""" |
| 78 | if not 1 <= month <= 12: |
| 79 | raise ValueError, 'bad month number' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 80 | day1 = weekday(year, month, 1) |
| 81 | ndays = mdays[month] + (month == February and isleap(year)) |
| 82 | return day1, ndays |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 83 | |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 84 | def monthcalendar(year, month): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 85 | """Return a matrix representing a month's calendar. |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 86 | Each row represents a week; days outside this month are zero.""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 87 | day1, ndays = monthrange(year, month) |
| 88 | rows = [] |
| 89 | r7 = range(7) |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 90 | day = (_firstweekday - day1 + 6) % 7 - 5 # for leading 0's in first week |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 91 | while day <= ndays: |
| 92 | row = [0, 0, 0, 0, 0, 0, 0] |
| 93 | for i in r7: |
| 94 | if 1 <= day <= ndays: row[i] = day |
| 95 | day = day + 1 |
| 96 | rows.append(row) |
| 97 | return rows |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 99 | def _center(str, width): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 100 | """Center a string in a field.""" |
| 101 | n = width - len(str) |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 102 | if n <= 0: |
| 103 | return str |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 104 | return ' '*((n+1)/2) + str + ' '*((n)/2) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 105 | |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 106 | def prweek(theweek, width): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 107 | """Print a single week (no newline).""" |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 108 | print week(theweek, width), |
| 109 | |
| 110 | def week(theweek, width): |
| 111 | """Returns a single week in a string (no newline).""" |
| 112 | days = [] |
| 113 | for day in theweek: |
| 114 | if day == 0: |
| 115 | s = '' |
| 116 | else: |
| 117 | s = '%2i' % day # right-align single-digit days |
| 118 | days.append(_center(s, width)) |
| 119 | return ' '.join(days) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 121 | def weekheader(width): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 122 | """Return a header for a week.""" |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 123 | if width >= 9: |
| 124 | names = day_name |
| 125 | else: |
| 126 | names = day_abbr |
| 127 | days = [] |
| 128 | for i in range(_firstweekday, _firstweekday + 7): |
| 129 | days.append(_center(names[i%7][:width], width)) |
| 130 | return ' '.join(days) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 131 | |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 132 | def prmonth(theyear, themonth, w=0, l=0): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 133 | """Print a month's calendar.""" |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 134 | print month(theyear, themonth, w, l), |
| 135 | |
| 136 | def month(theyear, themonth, w=0, l=0): |
| 137 | """Return a month's calendar string (multi-line).""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 138 | w = max(2, w) |
| 139 | l = max(1, l) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 140 | s = (_center(month_name[themonth] + ' ' + `theyear`, |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 141 | 7 * (w + 1) - 1).rstrip() + |
| 142 | '\n' * l + weekheader(w).rstrip() + '\n' * l) |
| 143 | for aweek in monthcalendar(theyear, themonth): |
| 144 | s = s + week(aweek, w).rstrip() + '\n' * l |
| 145 | return s[:-l] + '\n' |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 146 | |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 147 | # Spacing of month columns for 3-column year calendar |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 148 | _colwidth = 7*3 - 1 # Amount printed by prweek() |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 149 | _spacing = 6 # Number of spaces between columns |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 150 | |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 151 | def format3c(a, b, c, colwidth=_colwidth, spacing=_spacing): |
| 152 | """Prints 3-column formatting for year calendars""" |
| 153 | print format3cstring(a, b, c, colwidth, spacing) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 154 | |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 155 | def format3cstring(a, b, c, colwidth=_colwidth, spacing=_spacing): |
| 156 | """Returns a string formatted from 3 strings, centered within 3 columns.""" |
| 157 | return (_center(a, colwidth) + ' ' * spacing + _center(b, colwidth) + |
| 158 | ' ' * spacing + _center(c, colwidth)) |
| 159 | |
| 160 | def prcal(year, w=0, l=0, c=_spacing): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 161 | """Print a year's calendar.""" |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 162 | print calendar(year, w, l, c), |
| 163 | |
| 164 | def calendar(year, w=0, l=0, c=_spacing): |
| 165 | """Returns a year's calendar as a multi-line string.""" |
| 166 | w = max(2, w) |
| 167 | l = max(1, l) |
| 168 | c = max(2, c) |
| 169 | colwidth = (w + 1) * 7 - 1 |
| 170 | s = _center(`year`, colwidth * 3 + c * 2).rstrip() + '\n' * l |
| 171 | header = weekheader(w) |
| 172 | header = format3cstring(header, header, header, colwidth, c).rstrip() |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 173 | for q in range(January, January+12, 3): |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 174 | s = (s + '\n' * l + |
| 175 | format3cstring(month_name[q], month_name[q+1], month_name[q+2], |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 176 | colwidth, c).rstrip() + |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 177 | '\n' * l + header + '\n' * l) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 178 | data = [] |
| 179 | height = 0 |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 180 | for amonth in range(q, q + 3): |
| 181 | cal = monthcalendar(year, amonth) |
| 182 | if len(cal) > height: |
| 183 | height = len(cal) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 184 | data.append(cal) |
| 185 | for i in range(height): |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 186 | weeks = [] |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 187 | for cal in data: |
| 188 | if i >= len(cal): |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 189 | weeks.append('') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 190 | else: |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 191 | weeks.append(week(cal[i], w)) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 192 | s = s + format3cstring(weeks[0], weeks[1], weeks[2], |
Skip Montanaro | ad3bc44 | 2000-08-30 14:01:28 +0000 | [diff] [blame] | 193 | colwidth, c).rstrip() + '\n' * l |
| 194 | return s[:-l] + '\n' |
Guido van Rossum | b39aff8 | 1999-06-09 15:07:38 +0000 | [diff] [blame] | 195 | |
Guido van Rossum | b39aff8 | 1999-06-09 15:07:38 +0000 | [diff] [blame] | 196 | EPOCH = 1970 |
| 197 | def timegm(tuple): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 198 | """Unrelated but handy function to calculate Unix timestamp from GMT.""" |
| 199 | year, month, day, hour, minute, second = tuple[:6] |
| 200 | assert year >= EPOCH |
| 201 | assert 1 <= month <= 12 |
| 202 | days = 365*(year-EPOCH) + leapdays(EPOCH, year) |
| 203 | for i in range(1, month): |
| 204 | days = days + mdays[i] |
| 205 | if month > 2 and isleap(year): |
| 206 | days = days + 1 |
| 207 | days = days + day - 1 |
| 208 | hours = days*24 + hour |
| 209 | minutes = hours*60 + minute |
| 210 | seconds = minutes*60 + second |
| 211 | return seconds |