Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!lll-winken!uunet!tektronix!percival!parsely!agora!ihf1!bobd From: bobd@ihf1.UUCP (Bob Dietrich) Newsgroups: comp.lang.pascal Subject: Re: Day-of-week algorithm, please! Message-ID: <622@ihf1.UUCP> Date: 15 Apr 89 01:33:53 GMT References: <19065@adm.BRL.MIL> <2684@ilium.cs.swarthmore.edu> Reply-To: bobd@ihf1.UUCP (Bob Dietrich) Organization: Intel Corp., Hillsboro, Oregon Lines: 154 This procedure originally appeared in Pascal News, written by Arthur Sale if I remember correctly. The machine-dependent portion (see comments) and anything it references are not Arthur's fault but my own. The procedure uses Zeller's congruence. Bob Dietrich Intel Corporation, Hillsboro, Oregon (503) 696-2092 usenet: uunet!littlei!intelhf!ihf1!bobd or tektronix!tessi!agora!ihf1!bobd or tektronix!ogccse!omepd!ihf1!bobd or tektronix!psu-cs!omepd!ihf1!bobd ===================================== procedure timelog( var f: text); {*******} {---------------------------------------------------------------} { } { This procedure prints out a basic log-record on the f } { file. It avoids the well-known problems of American and } { English date conventions, and the 24-hour clock confusion. } { } {---------------------------------------------------------------} const alfalength = 10; type int = 0 .. 99; index = 1 .. alfalength; alfa = packed array[ index] of char; var year : 01..99; { two digits, 19xx assumed } month : 1..12; { month number } day : 1..31; { day in month } hour : 0..23; { 24-hour clock assumed } minute : 0..59; { minutes past the hour } second : 0..59; { seconds past the hour } today, now : alfa; adjyear : 00..99; { Jan & Feb are taken as } adjmonth : 1..12; { last months of prev year } weekday : 0..6; { 0=Sunday, 1=Monday, etc } adjustedhour : 0..12; { conventional-clock } function makeint( string: alfa; i: index): int; { converts a two character digit sequence to a small integer } begin if string[i] = ' ' then string[i] := '0'; makeint := (ord(string[i]) - ord('0')) * 10 + (ord( string[i+1]) - ord( '0')); end; {makeint} begin { The statements between here and the next comment should } { be replaced by the equivalent for your system. Note the } { ranges of the variables documented in the declarations. } time(now); date( today); year := makeint( today, 8); { month := makeint( today, 4); } case today[4] of { Unix returns 'DD Mmm YY ' } 'J': if today[5] = 'a' then month := 1 else if today[6] = 'n' then month := 6 else month := 7; 'F': month := 2; 'M': if today[6] = 'r' then month := 3 else month := 5; 'A': if today[5] = 'p' then month := 4 else month := 8; 'S': month := 9; 'O': month := 10; 'N': month := 11; 'D': month := 12 end; day := makeint( today, 1); hour := makeint( now, 2); minute := makeint( now, 5); second := makeint( now, 8); { writeln( f, 'time = ''', now, ''', date = ''', today, ''''); } {DEBUG} { writeln( f, year:1, '/', month:1, '/', day:1, ' ', } {DEBUG} { hour:1, ':', minute:1, ':', second:1); } {DEBUG} { This closes the machine-dependent part } { Compute the adjusted hour we use } adjustedhour:=hour mod 12; if (adjustedhour = 0) then adjustedhour:=12; { Adjust month and year information } if (month <= 2) then begin adjmonth:=month+10; adjyear:=year-1 end else begin adjmonth:=month-2; adjyear:=year end; { Zeller's congruence } weekday := (((26 * adjmonth - 2) div 10) + day + adjyear + (adjyear div 4) + 1) mod 7; { Write the timelog out } case weekday of 0: write(f,'Sunday'); 1: write(f,'Monday'); 2: write(f,'Tuesday'); 3: write(f,'Wednesday'); 4: write(f,'Thursday'); 5: write(f,'Friday'); 6: write(f,'Saturday') end; write(f,', ',(year+1900):4,' '); case month of 1: write(f,'January'); 2: write(f,'February'); 3: write(f,'March'); 4: write(f,'April'); 5: write(f,'May'); 6: write(f,'June'); 7: write(f,'July'); 8: write(f,'August'); 9: write(f,'September'); 10: write(f,'October'); 11: write(f,'November'); 12: write(f,'December') end; write(f,' ',day:2,' ',adjustedhour:2,':', (minute div 10):1,(minute mod 10):1,':', (second div 10):1,(second mod 10):1); if (hour >= 12) then begin writeln(f,' PM.') end else begin writeln(f,' AM.') end; end; {timelog}