blob: 13c8bbbd3f24e00a130de6c97afc7ed997a63536 [file] [log] [blame]
Guido van Rossumc6360141990-10-13 19:23:40 +00001# module calendar
2
3##############################
4# Calendar support functions #
5##############################
6
7# This is based on UNIX ctime() et al. (also Standard C and POSIX)
8# Subtle but crucial differences:
9# - the order of the elements of a 'struct tm' differs, to ease sorting
10# - months numbers are 1-12, not 0-11; month arrays have a dummy element 0
11# - Monday is the first day of the week (numbered 0)
12
13# These are really parameters of the 'time' module:
Guido van Rossum2db91351992-10-18 17:09:59 +000014epoch = 1970 # Time began on January 1 of this year (00:00:00 UTC)
Guido van Rossumc6360141990-10-13 19:23:40 +000015day_0 = 3 # The epoch begins on a Thursday (Monday = 0)
16
17# Return 1 for leap years, 0 for non-leap years
18def isleap(year):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000019 return year % 4 == 0 and (year % 100 <> 0 or year % 400 == 0)
Guido van Rossumc6360141990-10-13 19:23:40 +000020
21# Constants for months referenced later
22January = 1
23February = 2
24
25# Number of days per month (except for February in leap years)
Guido van Rossumeb231551992-07-09 11:05:12 +000026mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
Guido van Rossumc6360141990-10-13 19:23:40 +000027
28# Exception raised for bad input (with string parameter for details)
29error = 'calendar error'
30
31# Turn seconds since epoch into calendar time
32def gmtime(secs):
33 if secs < 0: raise error, 'negative input to gmtime()'
Guido van Rossumfea2af11993-01-04 09:16:51 +000034 secs = int(secs)
Guido van Rossumc6360141990-10-13 19:23:40 +000035 mins, secs = divmod(secs, 60)
36 hours, mins = divmod(mins, 60)
37 days, hours = divmod(hours, 24)
38 wday = (days + day_0) % 7
39 year = epoch
40 # XXX Most of the following loop can be replaced by one division
41 while 1:
42 yd = 365 + isleap(year)
43 if days < yd: break
44 days = days - yd
45 year = year + 1
46 yday = days
47 month = January
48 while 1:
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000049 md = mdays[month] + (month == February and isleap(year))
Guido van Rossumc6360141990-10-13 19:23:40 +000050 if days < md: break
51 days = days - md
52 month = month + 1
53 return year, month, days + 1, hours, mins, secs, yday, wday
54 # XXX Week number also?
55
56# Return number of leap years in range [y1, y2)
57# Assume y1 <= y2 and no funny (non-leap century) years
58def leapdays(y1, y2):
59 return (y2+3)/4 - (y1+3)/4
60
61# Inverse of gmtime():
Guido van Rossum2db91351992-10-18 17:09:59 +000062# Turn UTC calendar time (less yday, wday) into seconds since epoch
Guido van Rossumc6360141990-10-13 19:23:40 +000063def mktime(year, month, day, hours, mins, secs):
64 days = day - 1
65 for m in range(January, month): days = days + mdays[m]
66 if isleap(year) and month > February: days = days+1
67 days = days + (year-epoch)*365 + leapdays(epoch, year)
68 return ((days*24 + hours)*60 + mins)*60 + secs
69
70# Full and abbreviated names of weekdays
Guido van Rossumeb231551992-07-09 11:05:12 +000071day_name = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', \
72 'Friday', 'Saturday', 'Sunday']
73day_abbr = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
Guido van Rossumc6360141990-10-13 19:23:40 +000074
75# Full and abbreviated of months (1-based arrays!!!)
Guido van Rossumeb231551992-07-09 11:05:12 +000076month_name = ['', 'January', 'February', 'March', 'April', \
77 'May', 'June', 'July', 'August', \
78 'September', 'October', 'November', 'December']
79month_abbr = [' ', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', \
80 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
Guido van Rossumc6360141990-10-13 19:23:40 +000081
82# Zero-fill string to two positions (helper for asctime())
83def dd(s):
84 while len(s) < 2: s = '0' + s
85 return s
86
87# Blank-fill string to two positions (helper for asctime())
88def zd(s):
89 while len(s) < 2: s = ' ' + s
90 return s
91
92# Turn calendar time as returned by gmtime() into a string
93# (the yday parameter is for compatibility with gmtime())
94def asctime(year, month, day, hours, mins, secs, yday, wday):
95 s = day_abbr[wday] + ' ' + month_abbr[month] + ' ' + zd(`day`)
96 s = s + ' ' + dd(`hours`) + ':' + dd(`mins`) + ':' + dd(`secs`)
97 return s + ' ' + `year`
98
99# Localization: Minutes West from Greenwich
Guido van Rossum2db91351992-10-18 17:09:59 +0000100timezone = -2*60 # Middle-European time with DST on
101# timezone = 5*60 # EST (sigh -- THINK time() doesn't return UTC)
Guido van Rossumc6360141990-10-13 19:23:40 +0000102
103# Local time ignores DST issues for now -- adjust 'timezone' to fake it
104def localtime(secs):
105 return gmtime(secs - timezone*60)
106
107# UNIX-style ctime (except it doesn't append '\n'!)
108def ctime(secs):
109 return asctime(localtime(secs))
110
111######################
112# Non-UNIX additions #
113######################
114
115# Calendar printing etc.
116
117# Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12), day (1-31)
118def weekday(year, month, day):
119 secs = mktime(year, month, day, 0, 0, 0)
120 days = secs / (24*60*60)
121 return (days + day_0) % 7
122
123# Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month
124def monthrange(year, month):
125 day1 = weekday(year, month, 1)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000126 ndays = mdays[month] + (month == February and isleap(year))
Guido van Rossumc6360141990-10-13 19:23:40 +0000127 return day1, ndays
128
129# Return a matrix representing a month's calendar
130# Each row represents a week; days outside this month are zero
131def _monthcalendar(year, month):
132 day1, ndays = monthrange(year, month)
133 rows = []
134 r7 = range(7)
135 day = 1 - day1
136 while day <= ndays:
137 row = [0, 0, 0, 0, 0, 0, 0]
138 for i in r7:
139 if 1 <= day <= ndays: row[i] = day
140 day = day + 1
141 rows.append(row)
142 return rows
143
144# Caching interface to _monthcalendar
145mc_cache = {}
146def monthcalendar(year, month):
147 key = `year` + month_abbr[month]
148 try:
149 return mc_cache[key]
Guido van Rossumfea2af11993-01-04 09:16:51 +0000150 except KeyError:
Guido van Rossumc6360141990-10-13 19:23:40 +0000151 mc_cache[key] = ret = _monthcalendar(year, month)
152 return ret
153
154# Center a string in a field
155def center(str, width):
156 n = width - len(str)
157 if n < 0: return str
158 return ' '*(n/2) + str + ' '*(n-n/2)
159
160# XXX The following code knows that print separates items with space!
161
162# Print a single week (no newline)
163def prweek(week, width):
164 for day in week:
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000165 if day == 0: print ' '*width,
Guido van Rossumc6360141990-10-13 19:23:40 +0000166 else:
167 if width > 2: print ' '*(width-3),
168 if day < 10: print '',
169 print day,
170
171# Return a header for a week
172def weekheader(width):
173 str = ''
174 for i in range(7):
175 if str: str = str + ' '
176 str = str + day_abbr[i%7][:width]
177 return str
178
179# Print a month's calendar
180def prmonth(year, month):
181 print weekheader(3)
182 for week in monthcalendar(year, month):
183 prweek(week, 3)
184 print
185
186# Spacing between month columns
187spacing = ' '
188
189# 3-column formatting for year calendars
190def format3c(a, b, c):
191 print center(a, 20), spacing, center(b, 20), spacing, center(c, 20)
192
193# Print a year's calendar
194def prcal(year):
195 header = weekheader(2)
196 format3c('', `year`, '')
197 for q in range(January, January+12, 3):
198 print
199 format3c(month_name[q], month_name[q+1], month_name[q+2])
200 format3c(header, header, header)
201 data = []
202 height = 0
203 for month in range(q, q+3):
204 cal = monthcalendar(year, month)
205 if len(cal) > height: height = len(cal)
206 data.append(cal)
207 for i in range(height):
208 for cal in data:
209 if i >= len(cal):
210 print ' '*20,
211 else:
212 prweek(cal[i], 2)
213 print spacing,
214 print