1993-06-23 17:30:50 +08:00
|
|
|
###############################
|
|
|
|
# Calendar printing functions #
|
|
|
|
###############################
|
1990-10-14 03:23:40 +08:00
|
|
|
|
1993-06-23 17:30:50 +08:00
|
|
|
# Revision 2: uses funtions from built-in time module
|
1990-10-14 03:23:40 +08:00
|
|
|
|
1993-06-21 05:02:22 +08:00
|
|
|
# Import functions and variables from time module
|
1993-06-23 17:30:50 +08:00
|
|
|
from time import gmtime, localtime, mktime, asctime, ctime
|
1990-10-14 03:23:40 +08:00
|
|
|
|
1993-06-21 05:02:22 +08:00
|
|
|
# Exception raised for bad input (with string parameter for details)
|
|
|
|
error = 'calendar.error'
|
1993-06-17 20:38:10 +08:00
|
|
|
|
1993-06-21 05:02:22 +08:00
|
|
|
# Note when comparing these calendars to the ones printed by cal(1):
|
|
|
|
# My calendars have Monday as the first day of the week, and Sunday as
|
|
|
|
# the last! (I believe this is the European convention.)
|
1990-10-14 03:23:40 +08:00
|
|
|
|
|
|
|
# Constants for months referenced later
|
|
|
|
January = 1
|
|
|
|
February = 2
|
|
|
|
|
|
|
|
# Number of days per month (except for February in leap years)
|
1992-07-09 19:05:12 +08:00
|
|
|
mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
1990-10-14 03:23:40 +08:00
|
|
|
|
|
|
|
# Full and abbreviated names of weekdays
|
1992-07-09 19:05:12 +08:00
|
|
|
day_name = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', \
|
|
|
|
'Friday', 'Saturday', 'Sunday']
|
|
|
|
day_abbr = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
|
1990-10-14 03:23:40 +08:00
|
|
|
|
1993-06-23 17:30:50 +08:00
|
|
|
# Full and abbreviated names of months (1-based arrays!!!)
|
1992-07-09 19:05:12 +08:00
|
|
|
month_name = ['', 'January', 'February', 'March', 'April', \
|
|
|
|
'May', 'June', 'July', 'August', \
|
|
|
|
'September', 'October', 'November', 'December']
|
1993-06-23 17:30:50 +08:00
|
|
|
month_abbr = [' ', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', \
|
|
|
|
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
1990-10-14 03:23:40 +08:00
|
|
|
|
1993-06-21 05:02:22 +08:00
|
|
|
# Return 1 for leap years, 0 for non-leap years
|
|
|
|
def isleap(year):
|
|
|
|
return year % 4 == 0 and (year % 100 <> 0 or year % 400 == 0)
|
1990-10-14 03:23:40 +08:00
|
|
|
|
1993-06-21 05:02:22 +08:00
|
|
|
# Return number of leap years in range [y1, y2)
|
|
|
|
# Assume y1 <= y2 and no funny (non-leap century) years
|
|
|
|
def leapdays(y1, y2):
|
|
|
|
return (y2+3)/4 - (y1+3)/4
|
1990-10-14 03:23:40 +08:00
|
|
|
|
|
|
|
# Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12), day (1-31)
|
|
|
|
def weekday(year, month, day):
|
1993-06-21 05:02:22 +08:00
|
|
|
secs = mktime((year, month, day, 0, 0, 0, 0, 0, 0))
|
|
|
|
tuple = localtime(secs)
|
|
|
|
return tuple[6]
|
1990-10-14 03:23:40 +08:00
|
|
|
|
|
|
|
# Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month
|
|
|
|
def monthrange(year, month):
|
1993-06-21 05:02:22 +08:00
|
|
|
if not 1 <= month <= 12: raise ValueError, 'bad month number'
|
1990-10-14 03:23:40 +08:00
|
|
|
day1 = weekday(year, month, 1)
|
1992-01-02 03:35:13 +08:00
|
|
|
ndays = mdays[month] + (month == February and isleap(year))
|
1990-10-14 03:23:40 +08:00
|
|
|
return day1, ndays
|
|
|
|
|
|
|
|
# Return a matrix representing a month's calendar
|
|
|
|
# Each row represents a week; days outside this month are zero
|
|
|
|
def _monthcalendar(year, month):
|
|
|
|
day1, ndays = monthrange(year, month)
|
|
|
|
rows = []
|
|
|
|
r7 = range(7)
|
|
|
|
day = 1 - day1
|
|
|
|
while day <= ndays:
|
|
|
|
row = [0, 0, 0, 0, 0, 0, 0]
|
|
|
|
for i in r7:
|
|
|
|
if 1 <= day <= ndays: row[i] = day
|
|
|
|
day = day + 1
|
|
|
|
rows.append(row)
|
|
|
|
return rows
|
|
|
|
|
|
|
|
# Caching interface to _monthcalendar
|
1993-06-21 05:02:22 +08:00
|
|
|
_mc_cache = {}
|
1990-10-14 03:23:40 +08:00
|
|
|
def monthcalendar(year, month):
|
1993-06-21 05:02:22 +08:00
|
|
|
key = (year, month)
|
|
|
|
if _mc_cache.has_key(key):
|
|
|
|
return _mc_cache[key]
|
|
|
|
else:
|
|
|
|
_mc_cache[key] = ret = _monthcalendar(year, month)
|
1990-10-14 03:23:40 +08:00
|
|
|
return ret
|
|
|
|
|
|
|
|
# Center a string in a field
|
1993-06-21 05:02:22 +08:00
|
|
|
def _center(str, width):
|
1990-10-14 03:23:40 +08:00
|
|
|
n = width - len(str)
|
1993-06-21 05:02:22 +08:00
|
|
|
if n <= 0: return str
|
|
|
|
return ' '*((n+1)/2) + str + ' '*((n)/2)
|
1990-10-14 03:23:40 +08:00
|
|
|
|
|
|
|
# XXX The following code knows that print separates items with space!
|
|
|
|
|
|
|
|
# Print a single week (no newline)
|
|
|
|
def prweek(week, width):
|
|
|
|
for day in week:
|
1993-06-21 05:02:22 +08:00
|
|
|
if day == 0: s = ''
|
|
|
|
else: s = `day`
|
|
|
|
print _center(s, width),
|
1990-10-14 03:23:40 +08:00
|
|
|
|
|
|
|
# Return a header for a week
|
|
|
|
def weekheader(width):
|
|
|
|
str = ''
|
1993-06-21 05:02:22 +08:00
|
|
|
if width >= 9: names = day_name
|
|
|
|
else: names = day_abbr
|
1990-10-14 03:23:40 +08:00
|
|
|
for i in range(7):
|
|
|
|
if str: str = str + ' '
|
1993-06-21 05:02:22 +08:00
|
|
|
str = str + _center(names[i%7][:width], width)
|
1990-10-14 03:23:40 +08:00
|
|
|
return str
|
|
|
|
|
|
|
|
# Print a month's calendar
|
1994-08-01 19:34:53 +08:00
|
|
|
def prmonth(year, month, w = 0, l = 0):
|
1993-06-21 05:02:22 +08:00
|
|
|
w = max(2, w)
|
|
|
|
l = max(1, l)
|
|
|
|
print _center(month_name[month] + ' ' + `year`, 7*(w+1) - 1),
|
|
|
|
print '\n'*l,
|
|
|
|
print weekheader(w),
|
|
|
|
print '\n'*l,
|
1990-10-14 03:23:40 +08:00
|
|
|
for week in monthcalendar(year, month):
|
1993-06-21 05:02:22 +08:00
|
|
|
prweek(week, w)
|
|
|
|
print '\n'*l,
|
1990-10-14 03:23:40 +08:00
|
|
|
|
1993-06-21 05:02:22 +08:00
|
|
|
# Spacing of month columns
|
|
|
|
_colwidth = 7*3 - 1 # Amount printed by prweek()
|
|
|
|
_spacing = ' '*4 # Spaces between columns
|
1990-10-14 03:23:40 +08:00
|
|
|
|
|
|
|
# 3-column formatting for year calendars
|
|
|
|
def format3c(a, b, c):
|
1993-06-21 05:02:22 +08:00
|
|
|
print _center(a, _colwidth),
|
|
|
|
print _spacing,
|
|
|
|
print _center(b, _colwidth),
|
|
|
|
print _spacing,
|
|
|
|
print _center(c, _colwidth)
|
1990-10-14 03:23:40 +08:00
|
|
|
|
|
|
|
# Print a year's calendar
|
|
|
|
def prcal(year):
|
|
|
|
header = weekheader(2)
|
|
|
|
format3c('', `year`, '')
|
|
|
|
for q in range(January, January+12, 3):
|
|
|
|
print
|
|
|
|
format3c(month_name[q], month_name[q+1], month_name[q+2])
|
|
|
|
format3c(header, header, header)
|
|
|
|
data = []
|
|
|
|
height = 0
|
|
|
|
for month in range(q, q+3):
|
|
|
|
cal = monthcalendar(year, month)
|
|
|
|
if len(cal) > height: height = len(cal)
|
|
|
|
data.append(cal)
|
|
|
|
for i in range(height):
|
|
|
|
for cal in data:
|
|
|
|
if i >= len(cal):
|
1993-06-21 05:02:22 +08:00
|
|
|
print ' '*_colwidth,
|
1990-10-14 03:23:40 +08:00
|
|
|
else:
|
|
|
|
prweek(cal[i], 2)
|
1993-06-21 05:02:22 +08:00
|
|
|
print _spacing,
|
1990-10-14 03:23:40 +08:00
|
|
|
print
|