blob: 8d5681a01856e5f4f5c58db428184df4b0d571c7 [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 Rossum00245cf1999-05-03 18:07:40 +00008from time import localtime, mktime
Guido van Rossumc6360141990-10-13 19:23:40 +00009
10# Exception raised for bad input (with string parameter for details)
Guido van Rossum00245cf1999-05-03 18:07:40 +000011error = ValueError
Guido van Rossumc6360141990-10-13 19:23:40 +000012
Guido van Rossum9b3bc711993-06-20 21:02:22 +000013# 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
18January = 1
19February = 2
20
21# Number of days per month (except for February in leap years)
22mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
23
24# Full and abbreviated names of weekdays
25day_name = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', \
26 'Friday', 'Saturday', 'Sunday']
27day_abbr = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
28
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000029# Full and abbreviated names of months (1-based arrays!!!)
Guido van Rossum9b3bc711993-06-20 21:02:22 +000030month_name = ['', 'January', 'February', 'March', 'April', \
31 'May', 'June', 'July', 'August', \
32 'September', 'October', 'November', 'December']
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000033month_abbr = [' ', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', \
34 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
Guido van Rossum9b3bc711993-06-20 21:02:22 +000035
36# Return 1 for leap years, 0 for non-leap years
37def 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
42def leapdays(y1, y2):
43 return (y2+3)/4 - (y1+3)/4
44
Guido van Rossumc6360141990-10-13 19:23:40 +000045# Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12), day (1-31)
46def weekday(year, month, day):
Guido van Rossum9b3bc711993-06-20 21:02:22 +000047 secs = mktime((year, month, day, 0, 0, 0, 0, 0, 0))
48 tuple = localtime(secs)
49 return tuple[6]
Guido van Rossumc6360141990-10-13 19:23:40 +000050
51# Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month
52def monthrange(year, month):
Guido van Rossum9b3bc711993-06-20 21:02:22 +000053 if not 1 <= month <= 12: raise ValueError, 'bad month number'
Guido van Rossumc6360141990-10-13 19:23:40 +000054 day1 = weekday(year, month, 1)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000055 ndays = mdays[month] + (month == February and isleap(year))
Guido van Rossumc6360141990-10-13 19:23:40 +000056 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
60def _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 Rossum9b3bc711993-06-20 21:02:22 +000074_mc_cache = {}
Guido van Rossumc6360141990-10-13 19:23:40 +000075def monthcalendar(year, month):
Guido van Rossum9b3bc711993-06-20 21:02:22 +000076 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 Rossumc6360141990-10-13 19:23:40 +000081 return ret
82
83# Center a string in a field
Guido van Rossum9b3bc711993-06-20 21:02:22 +000084def _center(str, width):
Guido van Rossumc6360141990-10-13 19:23:40 +000085 n = width - len(str)
Guido van Rossum9b3bc711993-06-20 21:02:22 +000086 if n <= 0: return str
87 return ' '*((n+1)/2) + str + ' '*((n)/2)
Guido van Rossumc6360141990-10-13 19:23:40 +000088
89# XXX The following code knows that print separates items with space!
90
91# Print a single week (no newline)
92def prweek(week, width):
93 for day in week:
Guido van Rossum9b3bc711993-06-20 21:02:22 +000094 if day == 0: s = ''
95 else: s = `day`
96 print _center(s, width),
Guido van Rossumc6360141990-10-13 19:23:40 +000097
98# Return a header for a week
99def weekheader(width):
100 str = ''
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000101 if width >= 9: names = day_name
102 else: names = day_abbr
Guido van Rossumc6360141990-10-13 19:23:40 +0000103 for i in range(7):
104 if str: str = str + ' '
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000105 str = str + _center(names[i%7][:width], width)
Guido van Rossumc6360141990-10-13 19:23:40 +0000106 return str
107
108# Print a month's calendar
Guido van Rossumb6775db1994-08-01 11:34:53 +0000109def prmonth(year, month, w = 0, l = 0):
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000110 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 Rossumc6360141990-10-13 19:23:40 +0000116 for week in monthcalendar(year, month):
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000117 prweek(week, w)
118 print '\n'*l,
Guido van Rossumc6360141990-10-13 19:23:40 +0000119
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000120# Spacing of month columns
121_colwidth = 7*3 - 1 # Amount printed by prweek()
122_spacing = ' '*4 # Spaces between columns
Guido van Rossumc6360141990-10-13 19:23:40 +0000123
124# 3-column formatting for year calendars
125def format3c(a, b, c):
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000126 print _center(a, _colwidth),
127 print _spacing,
128 print _center(b, _colwidth),
129 print _spacing,
130 print _center(c, _colwidth)
Guido van Rossumc6360141990-10-13 19:23:40 +0000131
132# Print a year's calendar
133def 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 Rossum9b3bc711993-06-20 21:02:22 +0000149 print ' '*_colwidth,
Guido van Rossumc6360141990-10-13 19:23:40 +0000150 else:
151 prweek(cal[i], 2)
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000152 print _spacing,
Guido van Rossumc6360141990-10-13 19:23:40 +0000153 print