<bigwig> service: calendar


service {
  const vector string month_name = vector { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
  const html CalendarDoc = <html>
    <h1>Calendar</h1>
    <hr>
    <h3><continue type=button value=-12>&lt;&lt;</continue><continue type=button value=-1>&lt;</continue><[month]> <[year]><continue type=button value=1>&gt;</continue><continue type=button value=12>&gt;&gt;</continue></h3>
    <b>today:</b> <em><[today]></em>
    <br>
    <table border=1>
    <tr><th width=40>Mon</th><th width=40>Tues</th><th width=40>Wed</th><th width=40>Thur</th><th width=40>Fri</th><th width=40>Sat</th><th width=40>Sun</th></tr>
    <[next_tr]>
    </table>
    <br>
    <continue type=button value=0>Done</continue>
    
  </html>;
  const html WeekRowDoc = <html>
    <tr><[next_day]></tr>
    <[next_tr]>
  </html>;
  const html NoDayDoc = <html>
    <td>&nbsp;</td>
    <[next_day]>
  </html>;
  const html DayDoc = <html>
    <td><[day]><br>
    <[content]></td>
    <[next_day]>
  </html>;
  const html Thanks = <html>
    Thank you for using the Calendar.
  </html>;
  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;
  }
}