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