[BACK]Return to README CVS log [TXT][DIR] Up to [local] / src / usr.bin / cal

Annotation of src/usr.bin/cal/README, Revision 1.1.1.1

1.1       deraadt     1: The cal(1) date routines were written from scratch, basically from first
                      2: principles.  The algorithm for calculating the day of week from any
                      3: Gregorian date was "reverse engineered".  This was necessary as most of
                      4: the documented algorithms have to do with date calculations for other
                      5: calendars (e.g. julian) and are only accurate when converted to gregorian
                      6: within a narrow range of dates.
                      7:
                      8: 1 Jan 1 is a Saturday because that's what cal says and I couldn't change
                      9: that even if I was dumb enough to try.  From this we can easily calculate
                     10: the 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:
                     31: It must be noted that the number of leap years calculation is sensitive
                     32: to the date for which the leap year is being calculated.  A year that occurs
                     33: before the reformation is determined to be a leap year if its modulo of
                     34: 4 equals zero.  But after the reformation, a year is only a leap year if
                     35: its modulo of 4 equals zero and its modulo of 100 does not.  Of course,
                     36: there is an exception for these century years.  If the modulo of 400 equals
                     37: zero, then the year is a leap year anyway.  This is, in fact, what the
                     38: gregorian reformation was all about (a bit of error in the old algorithm
                     39: that caused the calendar to be inaccurate.)
                     40:
                     41: Once we have the day in year for the first of the month in question, the
                     42: rest is trivial.