[net.followup] Day-of-week algorithm: results.

richard@sequent.UUCP (02/13/84)

Okay, enough!

I've recieved over a dozen replies to my requset for a date algorithm.
Ten of them used an algorithm called Zeller's Congruence, or a variation
thereof.  There were several others, but I decided to try the biggie first.

The others had no discernable advantage in terms of simplicity, and at
least two of them were dependent upon the century - one was the algorithm
used to determine d.o.w. only relative to 1/1/1900.

Zeller's Congruence:

Variables:	dd = day of month,
		mm = month of year,
		yy = year within century,
		cc = century.
For instance, 12/6/1942 would be dd=6, mm=12, yy=42, cc=19.

Adjustments:	For purposes of the algorithm, Jan and Feb are considered
		to be thxDe last months of the previous year.  My 
		assumption is to make the leap-date analysis simpler.
	if ( mm < 3 )
	{	mm += 10;
		--yy;		/* This is necessary! */
	}
	else	mm -= 2;

Calculations:

	A = int( (13 * m - 2) / 5 );
	B = yy + int( yy / 4 );
	C = int( cc / 4 ) - ( 2 * cc );
	D = dd + A + B + C;
	dow = mod(D,7);		/* Pascal: D mod 7	C: D % 7	*/

Note: D has a good possibility of being negative due to C.  This might
cause problems, if mod function will retrun a negative number.

Results: Sunday = 0;
	 Monday = 1;
	 .
	 .
	 .
	 Saturday = 6.

To run through an example, lets again take 12/6/1942.  After the
adjustments have been made, we have  dd=6, mm=10, yy=42, cc=19.
A becomes 25,  B = 52,  C = -34, and D is 49.  49 modulo 7 is
0, which leaves us on a Sunday.  For those of you that need to
check your calendars for this date, don't bother.  It's right.
Okay?

Using my handy-dandy table-driven perpetual calendar, the algortihm
works from January 1st 1753  to  December 31st 2030.  I know of no
reason it shouldn't go beyond those limits (excepting the Gregorian to
Julian conversion...) but my perpetual calendar can't handle it.

Proof of the theorem is left as an exercise for the reader.

			from the confused and bleeding fingertips of
				...!sequent!richard