CALENDAR.wp5	version 3

(documentation for CALENDAR.WPM in WP 5.1 format)

by Ed <Peace B. Still>, 72477,260

This macro creates a calendar for an entire year -- any year from 1901 through 2099 by creating the proper secondary file and then merging it with CALENDAR.PF, a primary file with seven columns.

To create the calendar, clear both documents and just run the macro. The macro checks for clear documents and tells you if either is not clear.

HOW THE MACRO WORKS: If you know what day of the week January begins on, and whether the year is a leap year, you can figure the day any other month begins on. I do this by hard-coding each year's "offset" -- the number of blank days at the beginning of the calendar. For instance, 1989 has an offset of 0 because it begins on Sunday. Each month's offset can then be computed from January's by add the number of days more than 28 in the prior month.

The macro takes the number of days in the offset, reduces it to a number between 0 and 6, and then puts in a ^E[HPg] for each blank day, then inserts the numbers for each day, followed by ^E[HPg], saves that list, merges it with Calendar.pf, and cycles to the next month.

Now the hard part is how it figures out the offset for any year. The offset-computing portion is reproduced at the end of this documentation.

The Gregorian Calendar has a leap year every fourth year, except years that end in "00," with the further exception that years divisible by 400 are leap years. (You can see why a lawyer would write this macro. We just love exceptions to exceptions.) Thus we have 199-year run from 1901 through 2099 that regularly have leap years.

Any perpetual calendar will reveal there are also 28-year cycles. Each year begins one day of the week later than the previous year; however, if the previous year was a leap year, there is a two day progression.

To duplicate this action I have to make several calculations. First, I divide the year by 28 and take the remainder to get the year's position in the cycle. That result is then divided by 4 to get both the integer result and the remainder. If the remainder is 0, the target is a leap year. To get the offset, I take the number of leap year cycles (less one, if the target is a leap year), multiply by 5, add the number of years since the last leap year (add 4 if the target is a leap year), add 6, and take the remainder after dividing by 7.

Let me explain why I take some of these steps. I multiply by 5 to replicate the effect of the leap year on the orderly progression of days. I divide by 7 and use the remainder to get the offset. The exceptions for leap years have to be used because the effect of the leap year on the 1 January offset is not felt until the next year.

If I took the results so far and just divided by 7 (the number of days in the week) and took the remainder, that would tell me the day of the week on which January begins, except that it would give a result of 0 for Saturday. I need one less than that, so I add 6. If you are looking at the remainder of a division by 7, adding 6 has the same effect as subtracting 1, but the offset never goes below 0.

If you want to take a look at how this operates, this ARC file includes C3.wpm which will show you the offset and whether the target is a leap year.

Portion of Macro

{TEXT}0~{Del to EOP}{Enter}{Enter}{Enter}

Peace B. Still Calendar Maker{Enter}{Enter}

What year (1901-2099) to print? (Enter all four digits) ~

{IF}{VAR 0}>2099~{GO}year~{ELSE}

{IF}{VAR 0}<1901~{GO}year~

{END IF}{END IF}

{ASSIGN}4~{VAR 0}%28~ {;}number of years beyond last 28-year cycle~

{ASSIGN}6~{VAR 4}%4~ {;}years beyond leap year~

{ASSIGN}5~{VAR 4}/4~ {;}how many leap year cycles~

{IF}{VAR 6}=0~

{ASSIGN}5~{VAR 5}-1~{ASSIGN}6~4~{ASSIGN}3~1~

{ELSE}

{ASSIGN}3~0~

{END IF}

{ASSIGN}7~{VAR 5}*5~

{ASSIGN}7~{VAR 7}+{VAR 6}~

{ASSIGN}7~{VAR 7}+6~

{ASSIGN}1~{VAR 7}%7~ {;}offset~

Edward Still

72477,260