PROCEDURE display_day, disd (
  month, m: any of
      key
        january, jan, february, feb, march, mar, april, apr, may, june, jun, july, jul, august, aug, september
        sep, october, oct, november, nov, december, dec
      keyend
      integer 1..12
    anyend = $integer($substring($date(iso), 6, 2))
  day, d: integer 1..31 = $integer($substring($date(iso), 9, 2))
  year, y: integer 1..$max_integer = $integer($substring($date(iso), 1, 4))
  output, o: file = $output
  status)

" PURPOSE:
"   Display the day of the week for any given date.
" DESIGN:
"   Compute the day of the week using Julian and Gregorian calanders as neccessary.

"$format=no
  VAR
    days: array 1 .. 7 of string 0 .. 9
    months: array 1 .. 12 of string 0 .. 9
    max_days: array 1 .. 12 of integer
    offset: array 1 .. 12 of integer
    output_line: array 1 .. 2 of string
  VAREND

  months(1)='January'        ;  max_days(1)=31   ;  offset(1)=0
  months(2)='February'       ;  max_days(2)=28   ;  offset(2)=3
  months(3)='March'          ;  max_days(3)=31   ;  offset(3)=3
  months(4)='April'          ;  max_days(4)=30   ;  offset(4)=6
  months(5)='May'            ;  max_days(5)=31   ;  offset(5)=1
  months(6)='June'           ;  max_days(6)=30   ;  offset(6)=4
  months(7)='July'           ;  max_days(7)=31   ;  offset(7)=6
  months(8)='August'         ;  max_days(8)=31   ;  offset(8)=2
  months(9)='September'      ;  max_days(9)=30   ;  offset(9)=5
  months(10)='October'       ;  max_days(10)=31  ;  offset(10)=0
  months(11)='November'      ;  max_days(11)=30  ;  offset(11)=3
  months(12)='December'      ;  max_days(12)=31  ;  offset(12)=5

  days(1)='Friday'
  days(2)='Saturday'
  days(3)='Sunday'
  days(4)='Monday'
  days(5)='Tuesday'
  days(6)='Wednesday'
  days(7)='Thursday'
"$format=yes

  IF $generic_type(month)= key THEN
    test_month=$substring($string(month), 1, 3)
    FOR month = 1 TO 12 DO
      EXIT WHEN test_month = $substring($string($name(months(month))), 1, 3)
    FOREND
  IFEND

  yyyymmdd=year * 10000 + month * 100 + day
  today_yyyymmdd=$integer($substring($date(iso), 1, 4))* 10000 + $integer($substring($date(iso), 6, 2))* 100..
         + $integer($substring($date(iso), 9, 2))
  the_day=months(month) // ' ' // $strrep(day)// ', ' // $strrep(year)
  IF yyyymmdd < today_yyyymmdd THEN
    header=the_day // ' fell on a '
  ELSEIF yyyymmdd = today_yyyymmdd THEN
    header='Today is '
  ELSE
    header=the_day // ' will fall on a '
  IFEND

  IF yyyymmdd <= 17520902 THEN " date for Julian calendar "
    leap_year=$integer($mod(year, 4)=0)
    day_index=$mod((year+(year+3)/4+day+leap_year*$integer(month>=3)+offset(month))+5, 7) + 1
    IF day > (max_days(month) + leap_year * $integer(month=2)) THEN
      output_line(1)=the_day // ' is not a possible date.'
    ELSE
      output_line(1)=header // days(day_index) // ' - Julian (old) calendar.'
    IFEND
  IFEND

  IF (15821015 <= yyyymmdd) AND (yyyymmdd <> 17000229) THEN " date for Gregorian calendar "
    year=$mod(year, 400)
    leap_year=$integer(((year=0) OR ($mod(year, 4)=0) AND ($mod(year, 100)<>0)))
    day_index=$mod((year+(year+3)/4-(year-1)/100+day+leap_year*$integer(month>=3)+offset(month)), 7) + 1
    IF day > (max_days(month) + leap_year * $integer(month=2)) THEN
      output_line(1)=the_day // ' is not a possible date.'
    ELSEIF yyyymmdd <= 17520902 THEN
      output_line(2)=$substring('', 1, $size(header))// days(day_index) // ' - Gregorian (current) calendar.'
    ELSE
      output_line(1)=header // days(day_index) // '.'
    IFEND
  IFEND

  display_value output_line output=output

PROCEND display_day
