Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 5/3/83; site sequent.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxl!houxm!hogpc!houti!ariel!vax135!cornell!uw-beaver!tektronix!ogcvax!sequent!richard From: richard@sequent.UUCP Newsgroups: net.followup Subject: Day-of-week algorithm: results. Message-ID: <392@sequent.UUCP> Date: Mon, 13-Feb-84 06:11:00 EST Article-I.D.: sequent.392 Posted: Mon Feb 13 06:11:00 1984 Date-Received: Sat, 18-Feb-84 04:26:33 EST Organization: Sequent Computer Systems, Portland Lines: 61 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