Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!mcvax!kth!draken!umecs!zeus!christer From: christer@zeus.cs.umu.se (Christer Ericson) Newsgroups: comp.lang.pascal Subject: Re: Day-of-week algorithm, please! Message-ID: <838@umecs.cs.umu.se> Date: 14 Apr 89 11:16:13 GMT References: <19065@adm.BRL.MIL> <2684@ilium.cs.swarthmore.edu> Sender: news@umecs.cs.umu.se Reply-To: christer@zeus.cs.umu.se (Christer Ericson) Organization: Dep. of Inform.Proc.,University of Umea,Sweden Lines: 50 In article <2684@ilium.cs.swarthmore.edu> jackiw@ilium.UUCP (Nick Jackiw) writes: >I know this ain't too helpful, but... > >There's a simple (five term) non-iterative formula called Ziegler's >Congruence (or possible Zegler's Congruence --- or was it Confluence? :-)) >that returns 0..6 for the day of the week of any day since the last >MAJOR calendar adjustment (1818?). > The formula which you are refering to is called Zeller's Congruence. Actually, the first program our students of CS has to write [in a functional language called ML] is an implementation of this formula. Anyway, here it is: weekday=([2.6*m-0.2] + d + y + [y/4] + [c/4] - 2*c) mod 7 Where c,y,m,d denotes century, year (last two digits only), month, and day respectively. [x] is the integer part of x. The number in weekday is interpreted as 0=Sunday, 1=Monday, 2=Tuesday, ... In pascal it could look like this: FUNCTION Zeller(y,m,d:INTEGER):INTEGER; VAR c : INTEGER; BEGIN c:=y DIV 100; y:=y MOD 100; Zeller:=((26*m-2) DIV 10 + d + y + y DIV 4 + c DIV 4 - 2*c) MOD 7; END; Oh, there is a small 'trap' in this formula and that is that the parenthesis before the modula can be negative, and many computer modulas don't handle negative numbers the correct way, so add [say] 777 to the left side before doing the modula. > _ _|\____ Nick Jackiw | Visual Geometry Project | Math Department > / /_/ O> \ ------------+-------------------------+ Swarthmore College > | O> | 215-328-8225| jackiw@cs.swarthmore.edu| Swarthmore PA 19081 > \_Guernica_/ ------------+-------------------------+ USA /Christer +-------------------------------+-------------------------------------------+ | Christer Ericson | Inst. for Information Processing, | | Internet: christer@cs.umu.se | University of Umea, S-90187 UMEA, Sweden | +-------------------------------+-------------------------------------------+