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