%begin;  !Find day of week
%include "INC:UTIL.IMP";  !for DATE
%constinteger FACTOR=1
%integer Y,M,D,DAYS,I,J,SYM,NOW
%string(255) TEXT

%on 4,9 %start
  newline %and %stop %if event_event = 9
  printstring("What kind of a date is that?");  newline
  read symbol(sym) %while sym # nl
  -> again
%finish

%conststring(9)%array DAY(0:6) =
 "Sunday", "Monday", "Tuesday",
 "Wednesday", "Thursday", "Friday",
 "Saturday"

%conststring(9)%array MONTH(1:12) =
 "January", "February", "March", "April",
 "May", "June", "July", "August",
 "September", "October", "November", "December"

%routine SKIP
  read symbol(sym) %while next symbol = ' '
%end

%integerfn DAYS IN YEAR(%integer y)
  %result = 365 %if rem(y,4) # 0
  %result = 366 %if rem(y,100) # 0;  !good enough
  %result = 365
%end

%integerfn DAYS IN MONTH(%integer m,y)
%constbytearray DAYS(1:12) =
  31,0,31,30,31,30,31,31,30,31,30,31
  %result = days(m) %if m # 2
  %result = days in year(y)-(365-28)
%end

%integerfn DAYS SINCE TIME BEGAN(%integer d,m,y)
%integer i,j
  j = d-1
  j = j + days in year(i) %for i = 1900,1,y-1
  j = j + days in month(i,y) %for i = 1,1,m-1
  %result = j
%end

%integerfn TEXTTONUM(%string(255) mt)
%integer i
  %for i = 1,1,12 %cycle
    %result = i %if month(i) = mt
    %result = i %if substring(month(i),1,3) = mt
  %repeat
  %result = -1
%end

%integerfn AT(%integer i)
  %result = (charno(text,i)-'0')*10+charno(text,i+1)-'0'
%end

text = date
now = days since time began(at(1),at(4),at(7)+1900)
printstring("  Type a date this century in the form: 12/09/56 then RETURN")
newline
prompt("  Date: ")
again:
%cycle
  skip;  read(d)
  skip;  read symbol(sym)
  %signal 4 %if sym # '/'
  skip
  %if '0' <= nextsymbol <= '9' %start
    read(m)
  %else
    %signal 4 %unless 'a' <= nextsymbol!32 <= 'z'
    read symbol(sym)
    text = tostring(sym&95)
    %while 'a' <= nextsymbol <= 'z' %cycle
      read symbol(sym)
      text = text.tostring(sym!32)
    %repeat
    m = texttonum(text)
  %finish
  skip;  read symbol(sym)
  %signal 4 %if sym # '/'
  skip;  read(y)
  y = y+1900 %if 0 <= y <= 99
 !check values in range
  %unless 1 <= d <= 31 %start
    printstring("What month has");  write(d,1)
    printstring(" days in it?");  newline
    -> again
  %finish
  %unless 1 <= m <= 12 %start
    %if m >= 0 %start
      printstring("What year has");  write(m,1)
      printstring(" months in it?")
    %else
      printstring("No such month")
    %finish
    newline
    -> again
  %finish
  %unless 1900 <= y < 2000 %start
    printstring("This century please");  newline
    -> again
  %finish
  %unless d <= days in month(m,y) %start
    printstring(month(m));  write(y,1) %if m = 2
    printstring(" does not have");  write(d,1)
    printstring(" days in it");  newline
    -> again
  %finish
  days = days since time began(d,m,y)
{write(date,1);  newline
 !print day of week
  printstring("    The");  write(d,1)
  %if rem(d,10) = 1 %and d # 11 %then printstring("st") %c
  %else %if rem(d,10) = 2 %and d # 12 %then printstring("nd") %c
  %else printstring("th")
  printstring(" of ".month(m))
  write(y,1)
  %if days < now %then printstring(" was") %c
  %else %if days = now %then printstring(" is") %c
  %else printstring(" will be")
  printstring(" a ".day(rem(days+factor,7)))
  newline
%repeat
%endofprogram
