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