blob: b4e3aaddbfa7a2a368cac4b9178df7750613aae9 [file] [log] [blame]
Guido van Rossum5cfa5df1993-06-23 09:30:50 +00001###############################
2# Calendar printing functions #
3###############################
Guido van Rossumc6360141990-10-13 19:23:40 +00004
Guido van Rossum5cfa5df1993-06-23 09:30:50 +00005# Revision 2: uses funtions from built-in time module
Guido van Rossumc6360141990-10-13 19:23:40 +00006
Guido van Rossum9b3bc711993-06-20 21:02:22 +00007# Import functions and variables from time module
Guido van Rossum5cfa5df1993-06-23 09:30:50 +00008from time import gmtime, localtime, mktime, asctime, ctime
Guido van Rossum9b3bc711993-06-20 21:02:22 +00009from time import timezone, altzone, daylight, tzname
Guido van Rossumc6360141990-10-13 19:23:40 +000010
11# Exception raised for bad input (with string parameter for details)
Guido van Rossum9b3bc711993-06-20 21:02:22 +000012error = 'calendar.error'
Guido van Rossumc6360141990-10-13 19:23:40 +000013
Guido van Rossum9b3bc711993-06-20 21:02:22 +000014# Note when comparing these calendars to the ones printed by cal(1):
15# My calendars have Monday as the first day of the week, and Sunday as
16# the last! (I believe this is the European convention.)
17
18# Constants for months referenced later
19January = 1
20February = 2
21
22# Number of days per month (except for February in leap years)
23mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
24
25# Full and abbreviated names of weekdays
26day_name = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', \
27 'Friday', 'Saturday', 'Sunday']
28day_abbr = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
29
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000030# Full and abbreviated names of months (1-based arrays!!!)
Guido van Rossum9b3bc711993-06-20 21:02:22 +000031month_name = ['', 'January', 'February', 'March', 'April', \
32 'May', 'June', 'July', 'August', \
33 'September', 'October', 'November', 'December']
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000034month_abbr = [' ', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', \
35 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
Guido van Rossum9b3bc711993-06-20 21:02:22 +000036
37# Return 1 for leap years, 0 for non-leap years
38def isleap(year):
39 return year % 4 == 0 and (year % 100 <> 0 or year % 400 == 0)
40
41# Return number of leap years in range [y1, y2)
42# Assume y1 <= y2 and no funny (non-leap century) years
43def leapdays(y1, y2):
44 return (y2+3)/4 - (y1+3)/4
45
Guido van Rossumc6360141990-10-13 19:23:40 +000046# Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12), day (1-31)
47def weekday(year, month, day):
Guido van Rossum9b3bc711993-06-20 21:02:22 +000048 secs = mktime((year, month, day, 0, 0, 0, 0, 0, 0))
49 tuple = localtime(secs)
50 return tuple[6]
Guido van Rossumc6360141990-10-13 19:23:40 +000051
52# Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month
53def monthrange(year, month):
Guido van Rossum9b3bc711993-06-20 21:02:22 +000054 if not 1 <= month <= 12: raise ValueError, 'bad month number'
Guido van Rossumc6360141990-10-13 19:23:40 +000055 day1 = weekday(year, month, 1)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000056 ndays = mdays[month] + (month == February and isleap(year))
Guido van Rossumc6360141990-10-13 19:23:40 +000057 return day1, ndays
58
59# Return a matrix representing a month's calendar
60# Each row represents a week; days outside this month are zero
61def _monthcalendar(year, month):
62 day1, ndays = monthrange(year, month)
63 rows = []
64 r7 = range(7)
65 day = 1 - day1
66 while day <= ndays:
67 row = [0, 0, 0, 0, 0, 0, 0]
68 for i in r7:
69 if 1 <= day <= ndays: row[i] = day
70 day = day + 1
71 rows.append(row)
72 return rows
73
74# Caching interface to _monthcalendar
Guido van Rossum9b3bc711993-06-20 21:02:22 +000075_mc_cache = {}
Guido van Rossumc6360141990-10-13 19:23:40 +000076def monthcalendar(year, month):
Guido van Rossum9b3bc711993-06-20 21:02:22 +000077 key = (year, month)
78 if _mc_cache.has_key(key):
79 return _mc_cache[key]
80 else:
81 _mc_cache[key] = ret = _monthcalendar(year, month)
Guido van Rossumc6360141990-10-13 19:23:40 +000082 return ret
83
84# Center a string in a field
Guido van Rossum9b3bc711993-06-20 21:02:22 +000085def _center(str, width):
Guido van Rossumc6360141990-10-13 19:23:40 +000086 n = width - len(str)
Guido van Rossum9b3bc711993-06-20 21:02:22 +000087 if n <= 0: return str
88 return ' '*((n+1)/2) + str + ' '*((n)/2)
Guido van Rossumc6360141990-10-13 19:23:40 +000089
90# XXX The following code knows that print separates items with space!
91
92# Print a single week (no newline)
93def prweek(week, width):
94 for day in week:
Guido van Rossum9b3bc711993-06-20 21:02:22 +000095 if day == 0: s = ''
96 else: s = `day`
97 print _center(s, width),
Guido van Rossumc6360141990-10-13 19:23:40 +000098
99# Return a header for a week
100def weekheader(width):
101 str = ''
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000102 if width >= 9: names = day_name
103 else: names = day_abbr
Guido van Rossumc6360141990-10-13 19:23:40 +0000104 for i in range(7):
105 if str: str = str + ' '
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000106 str = str + _center(names[i%7][:width], width)
Guido van Rossumc6360141990-10-13 19:23:40 +0000107 return str
108
109# Print a month's calendar
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000110def prmonth(year, month, *rest):
111 if rest[2:]: raise TypeError, 'too many args'
112 w = 0
113 l = 0
114 if rest[0:]: w = rest[0]
115 if rest[1:]: l = rest[1]
116 w = max(2, w)
117 l = max(1, l)
118 print _center(month_name[month] + ' ' + `year`, 7*(w+1) - 1),
119 print '\n'*l,
120 print weekheader(w),
121 print '\n'*l,
Guido van Rossumc6360141990-10-13 19:23:40 +0000122 for week in monthcalendar(year, month):
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000123 prweek(week, w)
124 print '\n'*l,
Guido van Rossumc6360141990-10-13 19:23:40 +0000125
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000126# Spacing of month columns
127_colwidth = 7*3 - 1 # Amount printed by prweek()
128_spacing = ' '*4 # Spaces between columns
Guido van Rossumc6360141990-10-13 19:23:40 +0000129
130# 3-column formatting for year calendars
131def format3c(a, b, c):
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000132 print _center(a, _colwidth),
133 print _spacing,
134 print _center(b, _colwidth),
135 print _spacing,
136 print _center(c, _colwidth)
Guido van Rossumc6360141990-10-13 19:23:40 +0000137
138# Print a year's calendar
139def prcal(year):
140 header = weekheader(2)
141 format3c('', `year`, '')
142 for q in range(January, January+12, 3):
143 print
144 format3c(month_name[q], month_name[q+1], month_name[q+2])
145 format3c(header, header, header)
146 data = []
147 height = 0
148 for month in range(q, q+3):
149 cal = monthcalendar(year, month)
150 if len(cal) > height: height = len(cal)
151 data.append(cal)
152 for i in range(height):
153 for cal in data:
154 if i >= len(cal):
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000155 print ' '*_colwidth,
Guido van Rossumc6360141990-10-13 19:23:40 +0000156 else:
157 prweek(cal[i], 2)
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000158 print _spacing,
Guido van Rossumc6360141990-10-13 19:23:40 +0000159 print