README revision 1.1 1 1.1 cgd The cal(1) date routines were written from scratch, basically from first
2 1.1 cgd principles. The algorithm for calculating the day of week from any
3 1.1 cgd Gregorian date was "reverse engineered". This was necessary as most of
4 1.1 cgd the documented algorithms have to do with date calculations for other
5 1.1 cgd calendars (e.g. julian) and are only accurate when converted to gregorian
6 1.1 cgd within a narrow range of dates.
7 1.1 cgd
8 1.1 cgd 1 Jan 1 is a Saturday because that's what cal says and I couldn't change
9 1.1 cgd that even if I was dumb enough to try. From this we can easily calculate
10 1.1 cgd the day of week for any date. The algorithm for a zero based day of week:
11 1.1 cgd
12 1.1 cgd calculate the number of days in all prior years (year-1)*365
13 1.1 cgd add the number of leap years (days?) since year 1
14 1.1 cgd (not including this year as that is covered later)
15 1.1 cgd add the day number within the year
16 1.1 cgd this compensates for the non-inclusive leap year
17 1.1 cgd calculation
18 1.1 cgd if the day in question occurs before the gregorian reformation
19 1.1 cgd (3 sep 1752 for our purposes), then simply return
20 1.1 cgd (value so far - 1 + SATURDAY's value of 6) modulo 7.
21 1.1 cgd if the day in question occurs during the reformation (3 sep 1752
22 1.1 cgd to 13 sep 1752 inclusive) return THURSDAY. This is my
23 1.1 cgd idea of what happened then. It does not matter much as
24 1.1 cgd this program never tries to find day of week for any day
25 1.1 cgd that is not the first of a month.
26 1.1 cgd otherwise, after the reformation, use the same formula as the
27 1.1 cgd days before with the additional step of subtracting the
28 1.1 cgd number of days (11) that were adjusted out of the calendar
29 1.1 cgd just before taking the modulo.
30 1.1 cgd
31 1.1 cgd It must be noted that the number of leap years calculation is sensitive
32 1.1 cgd to the date for which the leap year is being calculated. A year that occurs
33 1.1 cgd before the reformation is determined to be a leap year if its modulo of
34 1.1 cgd 4 equals zero. But after the reformation, a year is only a leap year if
35 1.1 cgd its modulo of 4 equals zero and its modulo of 100 does not. Of course,
36 1.1 cgd there is an exception for these century years. If the modulo of 400 equals
37 1.1 cgd zero, then the year is a leap year anyway. This is, in fact, what the
38 1.1 cgd gregorian reformation was all about (a bit of error in the old algorithm
39 1.1 cgd that caused the calendar to be inaccurate.)
40 1.1 cgd
41 1.1 cgd Once we have the day in year for the first of the month in question, the
42 1.1 cgd rest is trivial.
43