service {
const vector string month_name = vector { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
const html CalendarDoc =
Calendar
<<<<[month]> <[year]>>>>
today: <[today]>
| Mon | Tues | Wed | Thur | Fri | Sat | Sun |
<[next_tr]>
Done
;
const html WeekRowDoc =
<[next_day]>
<[next_tr]>
;
const html NoDayDoc =
|
<[next_day]>
;
const html DayDoc =
<[day]>
<[content]> |
<[next_day]>
;
const html Thanks =
Thank you for using the Calendar.
;
string prettyMonth(time t) {
return month_name[(getMonth(t) - 1)];
}
string today() {
time t = now();
return prettyMonth(t) + " " + getDay(t) + ", " + getYear(t);
}
html showMonth(time t) {
int i, month, weekday;
time t2;
html H;
t2 = (setDay ( t , 1 ));
month = getMonth(t);
weekday = getWeekday(t2);
H = CalendarDoc <[month=prettyMonth(t),
year=getYear(t),
today=today()];
H = H <[next_tr=WeekRowDoc];
for (i = 1 ; i < weekday ; i++) H = H <[next_day=NoDayDoc];
while (getMonth(t2) == month) {
H = H <[next_day=DayDoc <[day=getDay(t2),
content=""]];
if (getWeekday(t2) == 7) H = H <[next_day="",
next_tr=WeekRowDoc];
t2 = (setDay ( t2 , getDay(t2) + 1 ));
}
for (i = getWeekday(t2) ; i > 1 && i <= 7 ; i++) H = H <[next_day=NoDayDoc];
return H;
}
session Calendar() {
int num_months;
time t;
t = (setDay ( now() , 1 ));
repeat {
show showMonth(t) receive [num_months=continue];
t = (setMonth ( t , getMonth(t) + num_months ));
if (t == notime) t = now();
} until ( num_months == 0 ) ;
exit Thanks;
}
}