1990-10-13 16:23:40 -03:00
|
|
|
# module calendar
|
|
|
|
|
|
|
|
##############################
|
|
|
|
# Calendar support functions #
|
|
|
|
##############################
|
|
|
|
|
|
|
|
# This is based on UNIX ctime() et al. (also Standard C and POSIX)
|
|
|
|
# Subtle but crucial differences:
|
|
|
|
# - the order of the elements of a 'struct tm' differs, to ease sorting
|
|
|
|
# - months numbers are 1-12, not 0-11; month arrays have a dummy element 0
|
|
|
|
# - Monday is the first day of the week (numbered 0)
|
|
|
|
|
|
|
|
# These are really parameters of the 'time' module:
|
1992-10-18 14:09:59 -03:00
|
|
|
epoch = 1970 # Time began on January 1 of this year (00:00:00 UTC)
|
1990-10-13 16:23:40 -03:00
|
|
|
day_0 = 3 # The epoch begins on a Thursday (Monday = 0)
|
|
|
|
|
|
|
|
# Return 1 for leap years, 0 for non-leap years
|
|
|
|
def isleap(year):
|
1992-01-01 15:35:13 -04:00
|
|
|
return year % 4 == 0 and (year % 100 <> 0 or year % 400 == 0)
|
1990-10-13 16:23:40 -03:00
|
|
|
|
|
|
|
# Constants for months referenced later
|
|
|
|
January = 1
|
|
|
|
February = 2
|
|
|
|
|
|
|
|
# Number of days per month (except for February in leap years)
|
1992-07-09 08:05:12 -03:00
|
|
|
mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
1990-10-13 16:23:40 -03:00
|
|
|
|
|
|
|
# Exception raised for bad input (with string parameter for details)
|
|
|
|
error = 'calendar error'
|
|
|
|
|
|
|
|
# Turn seconds since epoch into calendar time
|
|
|
|
def gmtime(secs):
|
|
|
|
if secs < 0: raise error, 'negative input to gmtime()'
|
|
|
|
mins, secs = divmod(secs, 60)
|
|
|
|
hours, mins = divmod(mins, 60)
|
|
|
|
days, hours = divmod(hours, 24)
|
|
|
|
wday = (days + day_0) % 7
|
|
|
|
year = epoch
|
|
|
|
# XXX Most of the following loop can be replaced by one division
|
|
|
|
while 1:
|
|
|
|
yd = 365 + isleap(year)
|
|
|
|
if days < yd: break
|
|
|
|
days = days - yd
|
|
|
|
year = year + 1
|
|
|
|
yday = days
|
|
|
|
month = January
|
|
|
|
while 1:
|
1992-01-01 15:35:13 -04:00
|
|
|
md = mdays[month] + (month == February and isleap(year))
|
1990-10-13 16:23:40 -03:00
|
|
|
if days < md: break
|
|
|
|
days = days - md
|
|
|
|
month = month + 1
|
|
|
|
return year, month, days + 1, hours, mins, secs, yday, wday
|
|
|
|
# XXX Week number also?
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# Inverse of gmtime():
|
1992-10-18 14:09:59 -03:00
|
|
|
# Turn UTC calendar time (less yday, wday) into seconds since epoch
|
1990-10-13 16:23:40 -03:00
|
|
|
def mktime(year, month, day, hours, mins, secs):
|
|
|
|
days = day - 1
|
|
|
|
for m in range(January, month): days = days + mdays[m]
|
|
|
|
if isleap(year) and month > February: days = days+1
|
|
|
|
days = days + (year-epoch)*365 + leapdays(epoch, year)
|
|
|
|
return ((days*24 + hours)*60 + mins)*60 + secs
|
|
|
|
|
|
|
|
# Full and abbreviated names of weekdays
|
1992-07-09 08:05:12 -03:00
|
|
|
day_name = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', \
|
|
|
|
'Friday', 'Saturday', 'Sunday']
|
|
|
|
day_abbr = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
|
1990-10-13 16:23:40 -03:00
|
|
|
|
|
|
|
# Full and abbreviated of months (1-based arrays!!!)
|
1992-07-09 08:05:12 -03:00
|
|
|
month_name = ['', 'January', 'February', 'March', 'April', \
|
|
|
|
'May', 'June', 'July', 'August', \
|
|
|
|
'September', 'October', 'November', 'December']
|
|
|
|
month_abbr = [' ', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', \
|
|
|
|
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
1990-10-13 16:23:40 -03:00
|
|
|
|
|
|
|
# Zero-fill string to two positions (helper for asctime())
|
|
|
|
def dd(s):
|
|
|
|
while len(s) < 2: s = '0' + s
|
|
|
|
return s
|
|
|
|
|
|
|
|
# Blank-fill string to two positions (helper for asctime())
|
|
|
|
def zd(s):
|
|
|
|
while len(s) < 2: s = ' ' + s
|
|
|
|
return s
|
|
|
|
|
|
|
|
# Turn calendar time as returned by gmtime() into a string
|
|
|
|
# (the yday parameter is for compatibility with gmtime())
|
|
|
|
def asctime(year, month, day, hours, mins, secs, yday, wday):
|
|
|
|
s = day_abbr[wday] + ' ' + month_abbr[month] + ' ' + zd(`day`)
|
|
|
|
s = s + ' ' + dd(`hours`) + ':' + dd(`mins`) + ':' + dd(`secs`)
|
|
|
|
return s + ' ' + `year`
|
|
|
|
|
|
|
|
# Localization: Minutes West from Greenwich
|
1992-10-18 14:09:59 -03:00
|
|
|
timezone = -2*60 # Middle-European time with DST on
|
|
|
|
# timezone = 5*60 # EST (sigh -- THINK time() doesn't return UTC)
|
1990-10-13 16:23:40 -03:00
|
|
|
|
|
|
|
# Local time ignores DST issues for now -- adjust 'timezone' to fake it
|
|
|
|
def localtime(secs):
|
|
|
|
return gmtime(secs - timezone*60)
|
|
|
|
|
|
|
|
# UNIX-style ctime (except it doesn't append '\n'!)
|
|
|
|
def ctime(secs):
|
|
|
|
return asctime(localtime(secs))
|
|
|
|
|
|
|
|
######################
|
|
|
|
# Non-UNIX additions #
|
|
|
|
######################
|
|
|
|
|
|
|
|
# Calendar printing etc.
|
|
|
|
|
|
|
|
# Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12), day (1-31)
|
|
|
|
def weekday(year, month, day):
|
|
|
|
secs = mktime(year, month, day, 0, 0, 0)
|
|
|
|
days = secs / (24*60*60)
|
|
|
|
return (days + day_0) % 7
|
|
|
|
|
|
|
|
# Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month
|
|
|
|
def monthrange(year, month):
|
|
|
|
day1 = weekday(year, month, 1)
|
1992-01-01 15:35:13 -04:00
|
|
|
ndays = mdays[month] + (month == February and isleap(year))
|
1990-10-13 16:23:40 -03: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
|
|
|
|
mc_cache = {}
|
|
|
|
def monthcalendar(year, month):
|
|
|
|
key = `year` + month_abbr[month]
|
|
|
|
try:
|
|
|
|
return mc_cache[key]
|
1991-12-26 09:02:59 -04:00
|
|
|
except IOError:
|
1990-10-13 16:23:40 -03:00
|
|
|
mc_cache[key] = ret = _monthcalendar(year, month)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
# Center a string in a field
|
|
|
|
def center(str, width):
|
|
|
|
n = width - len(str)
|
|
|
|
if n < 0: return str
|
|
|
|
return ' '*(n/2) + str + ' '*(n-n/2)
|
|
|
|
|
|
|
|
# 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:
|
1992-01-01 15:35:13 -04:00
|
|
|
if day == 0: print ' '*width,
|
1990-10-13 16:23:40 -03:00
|
|
|
else:
|
|
|
|
if width > 2: print ' '*(width-3),
|
|
|
|
if day < 10: print '',
|
|
|
|
print day,
|
|
|
|
|
|
|
|
# Return a header for a week
|
|
|
|
def weekheader(width):
|
|
|
|
str = ''
|
|
|
|
for i in range(7):
|
|
|
|
if str: str = str + ' '
|
|
|
|
str = str + day_abbr[i%7][:width]
|
|
|
|
return str
|
|
|
|
|
|
|
|
# Print a month's calendar
|
|
|
|
def prmonth(year, month):
|
|
|
|
print weekheader(3)
|
|
|
|
for week in monthcalendar(year, month):
|
|
|
|
prweek(week, 3)
|
|
|
|
print
|
|
|
|
|
|
|
|
# Spacing between month columns
|
|
|
|
spacing = ' '
|
|
|
|
|
|
|
|
# 3-column formatting for year calendars
|
|
|
|
def format3c(a, b, c):
|
|
|
|
print center(a, 20), spacing, center(b, 20), spacing, center(c, 20)
|
|
|
|
|
|
|
|
# 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):
|
|
|
|
print ' '*20,
|
|
|
|
else:
|
|
|
|
prweek(cal[i], 2)
|
|
|
|
print spacing,
|
|
|
|
print
|