blob: a2bd398f45f424b5a93fda30c13e4f3ccda8e012 [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())
Guido van Rossum995c33a1993-02-05 09:39:16 +000094def asctime(arg):
95 year, month, day, hours, mins, secs, yday, wday = arg
Guido van Rossumc6360141990-10-13 19:23:40 +000096 s = day_abbr[wday] + ' ' + month_abbr[month] + ' ' + zd(`day`)
97 s = s + ' ' + dd(`hours`) + ':' + dd(`mins`) + ':' + dd(`secs`)
98 return s + ' ' + `year`
99
100# Localization: Minutes West from Greenwich
Guido van Rossum2db91351992-10-18 17:09:59 +0000101timezone = -2*60 # Middle-European time with DST on
102# timezone = 5*60 # EST (sigh -- THINK time() doesn't return UTC)
Guido van Rossumc6360141990-10-13 19:23:40 +0000103
104# Local time ignores DST issues for now -- adjust 'timezone' to fake it
105def localtime(secs):
106 return gmtime(secs - timezone*60)
107
108# UNIX-style ctime (except it doesn't append '\n'!)
109def ctime(secs):
110 return asctime(localtime(secs))
111
112######################
113# Non-UNIX additions #
114######################
115
116# Calendar printing etc.
117
118# Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12), day (1-31)
119def weekday(year, month, day):
120 secs = mktime(year, month, day, 0, 0, 0)
121 days = secs / (24*60*60)
122 return (days + day_0) % 7
123
124# Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month
125def monthrange(year, month):
126 day1 = weekday(year, month, 1)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000127 ndays = mdays[month] + (month == February and isleap(year))
Guido van Rossumc6360141990-10-13 19:23:40 +0000128 return day1, ndays
129
130# Return a matrix representing a month's calendar
131# Each row represents a week; days outside this month are zero
132def _monthcalendar(year, month):
133 day1, ndays = monthrange(year, month)
134 rows = []
135 r7 = range(7)
136 day = 1 - day1
137 while day <= ndays:
138 row = [0, 0, 0, 0, 0, 0, 0]
139 for i in r7:
140 if 1 <= day <= ndays: row[i] = day
141 day = day + 1
142 rows.append(row)
143 return rows
144
145# Caching interface to _monthcalendar
146mc_cache = {}
147def monthcalendar(year, month):
148 key = `year` + month_abbr[month]
149 try:
150 return mc_cache[key]
Guido van Rossumfea2af11993-01-04 09:16:51 +0000151 except KeyError:
Guido van Rossumc6360141990-10-13 19:23:40 +0000152 mc_cache[key] = ret = _monthcalendar(year, month)
153 return ret
154
155# Center a string in a field
156def center(str, width):
157 n = width - len(str)
158 if n < 0: return str
159 return ' '*(n/2) + str + ' '*(n-n/2)
160
161# XXX The following code knows that print separates items with space!
162
163# Print a single week (no newline)
164def prweek(week, width):
165 for day in week:
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000166 if day == 0: print ' '*width,
Guido van Rossumc6360141990-10-13 19:23:40 +0000167 else:
168 if width > 2: print ' '*(width-3),
169 if day < 10: print '',
170 print day,
171
172# Return a header for a week
173def weekheader(width):
174 str = ''
175 for i in range(7):
176 if str: str = str + ' '
177 str = str + day_abbr[i%7][:width]
178 return str
179
180# Print a month's calendar
181def prmonth(year, month):
182 print weekheader(3)
183 for week in monthcalendar(year, month):
184 prweek(week, 3)
185 print
186
187# Spacing between month columns
188spacing = ' '
189
190# 3-column formatting for year calendars
191def format3c(a, b, c):
192 print center(a, 20), spacing, center(b, 20), spacing, center(c, 20)
193
194# Print a year's calendar
195def prcal(year):
196 header = weekheader(2)
197 format3c('', `year`, '')
198 for q in range(January, January+12, 3):
199 print
200 format3c(month_name[q], month_name[q+1], month_name[q+2])
201 format3c(header, header, header)
202 data = []
203 height = 0
204 for month in range(q, q+3):
205 cal = monthcalendar(year, month)
206 if len(cal) > height: height = len(cal)
207 data.append(cal)
208 for i in range(height):
209 for cal in data:
210 if i >= len(cal):
211 print ' '*20,
212 else:
213 prweek(cal[i], 2)
214 print spacing,
215 print