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