Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 1 | ############################### |
| 2 | # Calendar printing functions # |
| 3 | ############################### |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 5 | # Revision 2: uses funtions from built-in time module |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 7 | # Import functions and variables from time module |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 8 | from time import gmtime, localtime, mktime, asctime, ctime |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 9 | |
| 10 | # Exception raised for bad input (with string parameter for details) |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 11 | error = 'calendar.error' |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 13 | # Note when comparing these calendars to the ones printed by cal(1): |
| 14 | # My calendars have Monday as the first day of the week, and Sunday as |
| 15 | # the last! (I believe this is the European convention.) |
| 16 | |
| 17 | # Constants for months referenced later |
| 18 | January = 1 |
| 19 | February = 2 |
| 20 | |
| 21 | # Number of days per month (except for February in leap years) |
| 22 | mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] |
| 23 | |
| 24 | # Full and abbreviated names of weekdays |
| 25 | day_name = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', \ |
| 26 | 'Friday', 'Saturday', 'Sunday'] |
| 27 | day_abbr = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] |
| 28 | |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 29 | # Full and abbreviated names of months (1-based arrays!!!) |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 30 | month_name = ['', 'January', 'February', 'March', 'April', \ |
| 31 | 'May', 'June', 'July', 'August', \ |
| 32 | 'September', 'October', 'November', 'December'] |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 33 | month_abbr = [' ', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', \ |
| 34 | 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 35 | |
| 36 | # Return 1 for leap years, 0 for non-leap years |
| 37 | def isleap(year): |
| 38 | return year % 4 == 0 and (year % 100 <> 0 or year % 400 == 0) |
| 39 | |
| 40 | # Return number of leap years in range [y1, y2) |
| 41 | # Assume y1 <= y2 and no funny (non-leap century) years |
| 42 | def leapdays(y1, y2): |
| 43 | return (y2+3)/4 - (y1+3)/4 |
| 44 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 45 | # Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12), day (1-31) |
| 46 | def weekday(year, month, day): |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 47 | secs = mktime((year, month, day, 0, 0, 0, 0, 0, 0)) |
| 48 | tuple = localtime(secs) |
| 49 | return tuple[6] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 50 | |
| 51 | # Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month |
| 52 | def monthrange(year, month): |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 53 | if not 1 <= month <= 12: raise ValueError, 'bad month number' |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 54 | day1 = weekday(year, month, 1) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 55 | ndays = mdays[month] + (month == February and isleap(year)) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 56 | return day1, ndays |
| 57 | |
| 58 | # Return a matrix representing a month's calendar |
| 59 | # Each row represents a week; days outside this month are zero |
| 60 | def _monthcalendar(year, month): |
| 61 | day1, ndays = monthrange(year, month) |
| 62 | rows = [] |
| 63 | r7 = range(7) |
| 64 | day = 1 - day1 |
| 65 | while day <= ndays: |
| 66 | row = [0, 0, 0, 0, 0, 0, 0] |
| 67 | for i in r7: |
| 68 | if 1 <= day <= ndays: row[i] = day |
| 69 | day = day + 1 |
| 70 | rows.append(row) |
| 71 | return rows |
| 72 | |
| 73 | # Caching interface to _monthcalendar |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 74 | _mc_cache = {} |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 75 | def monthcalendar(year, month): |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 76 | key = (year, month) |
| 77 | if _mc_cache.has_key(key): |
| 78 | return _mc_cache[key] |
| 79 | else: |
| 80 | _mc_cache[key] = ret = _monthcalendar(year, month) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 81 | return ret |
| 82 | |
| 83 | # Center a string in a field |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 84 | def _center(str, width): |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 85 | n = width - len(str) |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 86 | if n <= 0: return str |
| 87 | return ' '*((n+1)/2) + str + ' '*((n)/2) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 88 | |
| 89 | # XXX The following code knows that print separates items with space! |
| 90 | |
| 91 | # Print a single week (no newline) |
| 92 | def prweek(week, width): |
| 93 | for day in week: |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 94 | if day == 0: s = '' |
| 95 | else: s = `day` |
| 96 | print _center(s, width), |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 97 | |
| 98 | # Return a header for a week |
| 99 | def weekheader(width): |
| 100 | str = '' |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 101 | if width >= 9: names = day_name |
| 102 | else: names = day_abbr |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 103 | for i in range(7): |
| 104 | if str: str = str + ' ' |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 105 | str = str + _center(names[i%7][:width], width) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 106 | return str |
| 107 | |
| 108 | # Print a month's calendar |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 109 | def prmonth(year, month, w = 0, l = 0): |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 110 | w = max(2, w) |
| 111 | l = max(1, l) |
| 112 | print _center(month_name[month] + ' ' + `year`, 7*(w+1) - 1), |
| 113 | print '\n'*l, |
| 114 | print weekheader(w), |
| 115 | print '\n'*l, |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 116 | for week in monthcalendar(year, month): |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 117 | prweek(week, w) |
| 118 | print '\n'*l, |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 119 | |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 120 | # Spacing of month columns |
| 121 | _colwidth = 7*3 - 1 # Amount printed by prweek() |
| 122 | _spacing = ' '*4 # Spaces between columns |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 123 | |
| 124 | # 3-column formatting for year calendars |
| 125 | def format3c(a, b, c): |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 126 | print _center(a, _colwidth), |
| 127 | print _spacing, |
| 128 | print _center(b, _colwidth), |
| 129 | print _spacing, |
| 130 | print _center(c, _colwidth) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 131 | |
| 132 | # Print a year's calendar |
| 133 | def prcal(year): |
| 134 | header = weekheader(2) |
| 135 | format3c('', `year`, '') |
| 136 | for q in range(January, January+12, 3): |
| 137 | print |
| 138 | format3c(month_name[q], month_name[q+1], month_name[q+2]) |
| 139 | format3c(header, header, header) |
| 140 | data = [] |
| 141 | height = 0 |
| 142 | for month in range(q, q+3): |
| 143 | cal = monthcalendar(year, month) |
| 144 | if len(cal) > height: height = len(cal) |
| 145 | data.append(cal) |
| 146 | for i in range(height): |
| 147 | for cal in data: |
| 148 | if i >= len(cal): |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 149 | print ' '*_colwidth, |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 150 | else: |
| 151 | prweek(cal[i], 2) |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 152 | print _spacing, |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 153 | print |