
! -- Date and time stuff. Kdate, Kday pinched from EMAS
! -- J. Butler Oct 1984

{o}{%external }%integerfn elapsed time
{o}   !This function is predefined in the new operating system.
{o}   %result = cputime
{o}%end


{%external}%routine delay(%integer msec)
   !Wait in a tight loop for <msec> milliseconds. Note that because of the error
   !on elapsed time, the wait will be for ( <msec>  + 0.5) +/- 0.5 milliseconds.
   !The intention is to make this routine suspend the process rather than loop
   !it in the new operating system.
   %integer temp

   temp = elapsed time + msec
   %while elapsed time <= temp %cycle; %repeat
%end


{%external}%routine kdate(%integername d,m,y,%integer k)
!    k is days since 1st jan 1900
!    returns d, m, y   2 digit y only
      %integer w
      k=k+693902; ! days since Cleopatras birthday
      w=4*k-1
      y=w//146097
      k=w-146097*y
      d=k//4
      k=(4*d+3)//1461
      d=4*d+3-1461*k
      d=(d+4)//4
      m=(5*d-3)//153
      d=5*d-3-153*m
      d=(d+5)//5
      y=k
      %if m<10 %then m=m+3 %else %start
         m=m-9
         %if y=99 %then y = 0 %else y=y+1
      %finish
%end; ! of kdate



{%external}%integerfn kday(%integer d,m,y)
   !Kday = days since 1/1/1900. Remainder kday/7 = day of week (0 = Mon).
   %if m>2 %then m=m-3 %else m=m+9 %and y=y-1
   %result=1461*y//4+(153*m+2)//5+d+58
%end; ! of kday



%conststring(9)%array day(0:6) =
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
%conststring(3)%array mon(1:12) =
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
%conststring(2)%array ord(1:3) = "st", "nd", "rd"

%endoffile
