home *** CD-ROM | disk | FTP | other *** search
/ A.C.E. 1 / ACE CD 1.iso / files / utils / acroarex.dms / in.adf / Examples / month.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  1994-06-09  |  5.7 KB  |  223 lines

  1. /*
  2.  * cal - open a month's calendar on the workbench screen for the
  3.  *   user.
  4.  *
  5.  * usage: cal [month [year]]
  6.  *
  7.  * If no args are given, use the current month & year.
  8.  *
  9.  * Month may be either a month name (only the first three characters
  10.  * are used), or the number of the month.
  11.  *
  12.  * Year is the year of the christian era. If the length is exactly two
  13.  * characters, then a '19' is prepended to it. So '9' is the year 9,
  14.  * '09' is the year 1909; '34' is the year 1934, 034 is the year 34.
  15.  *
  16.  * The calendar presented is correct for England & its colonies for
  17.  * periods when there was more than one calendar in general use. For
  18.  * grins, try 'cal 9 1752'
  19.  *
  20.  * Mike Meyer, 1989
  21.  */
  22.  
  23. arg month year .
  24.  
  25. /* Set up the months table - from names to numbers, */
  26. months. = 0
  27. months.jan = 1
  28. months.feb = 2
  29. months.mar = 3
  30. months.apr = 4
  31. months.may = 5
  32. months.jun = 6
  33. months.jul = 7
  34. months.aug = 8
  35. months.sep = 9
  36. months.oct = 10
  37. months.nov = 11
  38. months.dec = 12
  39.  
  40. /* and now from numbers to days/month & print names */
  41. months.1 = 'January'
  42. months.1.days = 31
  43. months.2 = 'February'
  44. months.2.days = 1   /* Fixed later */
  45. months.3 = 'March'
  46. months.3.days = 31
  47. months.4 = 'April'
  48. months.4.days = 30
  49. months.5 = 'May'
  50. months.5.days = 31
  51. months.6 = 'June'
  52. months.6.days = 30
  53. months.7 = 'July'
  54. months.7.days = 31
  55. months.8 = 'August'
  56. months.8.days = 31
  57. months.9 = 'September'
  58. months.9.days = 30
  59. months.10 = 'October'
  60. months.10.days = 31
  61. months.11 = 'November'
  62. months.11.days = 30
  63. months.12 = 'December'
  64. months.12.days = 31      /* Not needed, but here for completeness */
  65.  
  66. /* Get the current date for later use*/
  67. parse value date('Normal') with . mymonth myyear
  68.  
  69. /* Get a month to work with */
  70. if datatype(month, 'Numeric') then mymonth = month
  71. else do
  72.    if month ~= "" then mymonth = month
  73.    mymonth = upper(left(mymonth, 3))
  74.    mymonth = months.mymonth
  75.    end
  76.  
  77. if months.mymonth.days = 0 then do
  78.    say "Month must be a month name or a number from 1 to 12, not" month
  79.    exit 10
  80.    end
  81.  
  82. /* Got a valid month, now see about the year */
  83. select
  84.    when year = "" then nop   /* myyear is already right */
  85.    when ~datatype(year, 'Numeric') then do
  86.       say "Year must be a number between 1 and 9999, not" year
  87.       exit 10
  88.       end
  89.    when length(year) = 2 then myyear = '19'year
  90.    otherwise myyear = year
  91.    end
  92.  
  93. if myyear < 1 | myyear > 9999 then do
  94.    say "Year must be between 1 and 9999 inclusive, not" myyear
  95.    exit 10
  96.    end
  97.  
  98. /* Figure out what day of the week that month started on */
  99. firstday = jan1(myyear)
  100.  
  101. /* Get difference in weekdays between this year & next */
  102. fudge = (jan1(myyear + 1) + 7 - firstday) // 7
  103.  
  104. select
  105.    /* this is a regular year */
  106.    when fudge = 1 then months.2.days = 28
  107.  
  108.    /* This is a leap year */
  109.    when fudge = 2 then months.2.days = 29
  110.  
  111.    /* Otherwise, it must be 1752! */
  112.    otherwise
  113.       months.2.days = 29
  114.       months.9.days = 19
  115.    end
  116.  
  117. do i = 1 to mymonth - 1
  118.    firstday = firstday + months.i.days
  119.    end
  120.  
  121. firstday = firstday // 7      /* Got the day of the week */
  122.  
  123. /*
  124.  * Now, go from that to the name of a day of the week. This table is also
  125.  * used for formatting the output. The line at the top of the body consists
  126.  * of these things concatenated together, with a space in between them.
  127.  * The length of that string is the width of the calendar. Finally, we
  128.  * line the numbers up under the last character of each name. All names
  129.  * _must_ be the same length for this to work.
  130.  */
  131. daynames.0 = 'Sun'
  132. daynames.1 = 'Mon'
  133. daynames.2 = 'Tue'
  134. daynames.3 = 'Wed'
  135. daynames.4 = 'Thu'
  136. daynames.5 = 'Fri'
  137. daynames.6 = 'Sat'
  138.  
  139. firstday = daynames.firstday      /* and now it's name */
  140.  
  141. /* Get number of days in this month */
  142. days = months.mymonth.days
  143.  
  144. /* Next, we set up the header for the calendar. */
  145. headerline = daynames.0
  146. do i = 1 to 6
  147.    headerline = headerline daynames.i
  148.    end
  149. linelength = length(headerline)         /* width of calendar */
  150.  
  151. /* Set up the header for the calender */
  152. lines.1 = center(months.mymonth myyear, linelength)
  153. lines.2 = " "
  154. lines.3 = headerline
  155. linecount = 4   /* First line of body of calendar */
  156.  
  157. /* Now set up to put together lines of the body */
  158. maxline = linecount + 5         /* 6 weeks on a monthly calendar, max */
  159. do i = linecount + 1 to maxline
  160.    lines.i = ""
  161.    end
  162.  
  163. width = length(daynames.0)
  164. lines.linecount = right(1, index(headerline, firstday) - 1 + width)
  165. do i = 2 to days
  166.    if i > 2 & days < 20 then day = i + 11
  167.    else day = i
  168.  
  169.    if length(lines.linecount) + width <= linelength then
  170.       lines.linecount = lines.linecount right(day, width)
  171.    else do
  172.       linecount = linecount + 1
  173.       lines.linecount = right(day, width)
  174.       end
  175.    end
  176.  
  177. /* now display it - in a window if possible, to the console if not */
  178. if ~show('Libraries', 'rexxarplib.library') then do
  179.    if ~addlib('rexxarplib.library', 0, -30) then do
  180.       /* Now rexxarplib, just dump it to the console */
  181.       do i = 1 to linecount
  182.          say lines.i
  183.          end
  184.       exit
  185.       end
  186.    end
  187.  
  188. /* Got a rexxarplib, so put it in a window */
  189. outline = lines.1
  190. do i = 2 to maxline
  191.    outline = outline '\'lines.i
  192.    end
  193.  
  194. /*
  195.  * We tack a space onto the end here, and in each line, so they will be
  196.  * cleared by postmsg. Sigh.
  197.  */
  198. call postmsg 0,0, outline ""
  199. exit
  200.  
  201. /*
  202.  * jan1 - returns the day of the week that january first falls on for
  203.  *   any specific year, 1 through 9999 (assuming they don't change
  204.  *   the rules again).
  205.  */
  206. jan1: procedure
  207.    arg year
  208.  
  209.    /* Julian calendar; one extra day every four years */
  210.    day = 4 + year + (year + 3) % 4
  211.  
  212.    /* Gregorian calendar - lose three days over four centuries */
  213.    if year > 1800 then do
  214.       day = day - (year - 1701) % 100
  215.       day = day + (year - 1601) % 400
  216.       end
  217.  
  218.    /* And the instant changeover in 1752 */
  219.    if year > 1752 then
  220.       day = day + 3
  221.  
  222.    return day // 7
  223.