blob: 9ef9e3d5431f94fc0ae88c5cfc169481057bb9ff [file] [log] [blame]
Guido van Rossumc6360141990-10-13 19:23:40 +00001##############################
2# Calendar support functions #
3##############################
4
Guido van Rossum9b3bc711993-06-20 21:02:22 +00005# Revision 2: uses funtions from built-in time module where possible.
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
8from time import gmtime, localtime, mktime
9from 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# Abbreviated names of months (1-based arrays!!!)
Guido van Rossumeb231551992-07-09 11:05:12 +000015month_abbr = [' ', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', \
16 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
Guido van Rossumc6360141990-10-13 19:23:40 +000017
Guido van Rossum9b3bc711993-06-20 21:02:22 +000018# Turn calendar time as returned by localtime() into a string
Guido van Rossum995c33a1993-02-05 09:39:16 +000019def asctime(arg):
Guido van Rossum9b3bc711993-06-20 21:02:22 +000020 year, month, day, hours, mins, secs, wday, yday, isdst = arg
21 return '%s %s %02d %02d:%02d:%02d %04d' % (
22 day_abbr[wday], month_abbr[month], day,
23 hours, mins, secs, year)
Guido van Rossumc6360141990-10-13 19:23:40 +000024
25# UNIX-style ctime (except it doesn't append '\n'!)
26def ctime(secs):
27 return asctime(localtime(secs))
28
29######################
30# Non-UNIX additions #
31######################
32
33# Calendar printing etc.
34
Guido van Rossum9b3bc711993-06-20 21:02:22 +000035# Note when comparing these calendars to the ones printed by cal(1):
36# My calendars have Monday as the first day of the week, and Sunday as
37# the last! (I believe this is the European convention.)
38
39# Constants for months referenced later
40January = 1
41February = 2
42
43# Number of days per month (except for February in leap years)
44mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
45
46# Full and abbreviated names of weekdays
47day_name = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', \
48 'Friday', 'Saturday', 'Sunday']
49day_abbr = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
50
51# Full names of months (1-based arrays!!!)
52month_name = ['', 'January', 'February', 'March', 'April', \
53 'May', 'June', 'July', 'August', \
54 'September', 'October', 'November', 'December']
55
56# Return 1 for leap years, 0 for non-leap years
57def isleap(year):
58 return year % 4 == 0 and (year % 100 <> 0 or year % 400 == 0)
59
60# Return number of leap years in range [y1, y2)
61# Assume y1 <= y2 and no funny (non-leap century) years
62def leapdays(y1, y2):
63 return (y2+3)/4 - (y1+3)/4
64
Guido van Rossumc6360141990-10-13 19:23:40 +000065# Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12), day (1-31)
66def weekday(year, month, day):
Guido van Rossum9b3bc711993-06-20 21:02:22 +000067 secs = mktime((year, month, day, 0, 0, 0, 0, 0, 0))
68 tuple = localtime(secs)
69 return tuple[6]
Guido van Rossumc6360141990-10-13 19:23:40 +000070
71# Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month
72def monthrange(year, month):
Guido van Rossum9b3bc711993-06-20 21:02:22 +000073 if not 1 <= month <= 12: raise ValueError, 'bad month number'
Guido van Rossumc6360141990-10-13 19:23:40 +000074 day1 = weekday(year, month, 1)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000075 ndays = mdays[month] + (month == February and isleap(year))
Guido van Rossumc6360141990-10-13 19:23:40 +000076 return day1, ndays
77
78# Return a matrix representing a month's calendar
79# Each row represents a week; days outside this month are zero
80def _monthcalendar(year, month):
81 day1, ndays = monthrange(year, month)
82 rows = []
83 r7 = range(7)
84 day = 1 - day1
85 while day <= ndays:
86 row = [0, 0, 0, 0, 0, 0, 0]
87 for i in r7:
88 if 1 <= day <= ndays: row[i] = day
89 day = day + 1
90 rows.append(row)
91 return rows
92
93# Caching interface to _monthcalendar
Guido van Rossum9b3bc711993-06-20 21:02:22 +000094_mc_cache = {}
Guido van Rossumc6360141990-10-13 19:23:40 +000095def monthcalendar(year, month):
Guido van Rossum9b3bc711993-06-20 21:02:22 +000096 key = (year, month)
97 if _mc_cache.has_key(key):
98 return _mc_cache[key]
99 else:
100 _mc_cache[key] = ret = _monthcalendar(year, month)
Guido van Rossumc6360141990-10-13 19:23:40 +0000101 return ret
102
103# Center a string in a field
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000104def _center(str, width):
Guido van Rossumc6360141990-10-13 19:23:40 +0000105 n = width - len(str)
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000106 if n <= 0: return str
107 return ' '*((n+1)/2) + str + ' '*((n)/2)
Guido van Rossumc6360141990-10-13 19:23:40 +0000108
109# XXX The following code knows that print separates items with space!
110
111# Print a single week (no newline)
112def prweek(week, width):
113 for day in week:
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000114 if day == 0: s = ''
115 else: s = `day`
116 print _center(s, width),
Guido van Rossumc6360141990-10-13 19:23:40 +0000117
118# Return a header for a week
119def weekheader(width):
120 str = ''
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000121 if width >= 9: names = day_name
122 else: names = day_abbr
Guido van Rossumc6360141990-10-13 19:23:40 +0000123 for i in range(7):
124 if str: str = str + ' '
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000125 str = str + _center(names[i%7][:width], width)
Guido van Rossumc6360141990-10-13 19:23:40 +0000126 return str
127
128# Print a month's calendar
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000129def prmonth(year, month, *rest):
130 if rest[2:]: raise TypeError, 'too many args'
131 w = 0
132 l = 0
133 if rest[0:]: w = rest[0]
134 if rest[1:]: l = rest[1]
135 w = max(2, w)
136 l = max(1, l)
137 print _center(month_name[month] + ' ' + `year`, 7*(w+1) - 1),
138 print '\n'*l,
139 print weekheader(w),
140 print '\n'*l,
Guido van Rossumc6360141990-10-13 19:23:40 +0000141 for week in monthcalendar(year, month):
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000142 prweek(week, w)
143 print '\n'*l,
Guido van Rossumc6360141990-10-13 19:23:40 +0000144
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000145# Spacing of month columns
146_colwidth = 7*3 - 1 # Amount printed by prweek()
147_spacing = ' '*4 # Spaces between columns
Guido van Rossumc6360141990-10-13 19:23:40 +0000148
149# 3-column formatting for year calendars
150def format3c(a, b, c):
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000151 print _center(a, _colwidth),
152 print _spacing,
153 print _center(b, _colwidth),
154 print _spacing,
155 print _center(c, _colwidth)
Guido van Rossumc6360141990-10-13 19:23:40 +0000156
157# Print a year's calendar
158def prcal(year):
159 header = weekheader(2)
160 format3c('', `year`, '')
161 for q in range(January, January+12, 3):
162 print
163 format3c(month_name[q], month_name[q+1], month_name[q+2])
164 format3c(header, header, header)
165 data = []
166 height = 0
167 for month in range(q, q+3):
168 cal = monthcalendar(year, month)
169 if len(cal) > height: height = len(cal)
170 data.append(cal)
171 for i in range(height):
172 for cal in data:
173 if i >= len(cal):
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000174 print ' '*_colwidth,
Guido van Rossumc6360141990-10-13 19:23:40 +0000175 else:
176 prweek(cal[i], 2)
Guido van Rossum9b3bc711993-06-20 21:02:22 +0000177 print _spacing,
Guido van Rossumc6360141990-10-13 19:23:40 +0000178 print