[comp.lang.pascal] Algorithm needed

moconnor@vela.acs.oakland.edu (Mike O'Connor) (10/10/90)

Could someone please mail me code such that given a date (something
like, say, October 10th, 1990), it tells me the day of the week?

Am demonstrating laziness...  oh well...

						...Mike


-- 
--              moconnor@argo.acs.oakland.edu           Internet     
--              moconnor@oakland                        Bitnet
--              ...!uunet!umich!egrunix!moconnor        UUCP
--

ts@uwasa.fi (Timo Salmi) (10/11/90)

In article <3334@vela.acs.oakland.edu> moconnor@argo.acs.oakland.edu writes:
>Could someone please mail me code such that given a date (something
>like, say, October 10th, 1990), it tells me the day of the week?
>Am demonstrating laziness...  oh well...

3. *****
 Q: I want code that gives the weekday of the given date.

 A1: There is a wkdayfn function in /pc/ts/tspas22.arc Turbo Pascal
units collection to give the modern weekday based on Zeller's
congruence. Also you can find a more extensive Julian and Gregorian
weekday algorithm in Dr.Dobbs Journal, June 1989, p. 148. Furthermore
Press & Flannery & al, Numerical Recipes, has the weekday code.

 A2: Some will recommend the following kludge.  Store the current
date, change it, and let MsDos get you the weekday.  This is a bad
suggestion.  On top of being sloppy programming, there are several
snags.  The trick works only for years 1980-2079.  A crash the
program may leave the clock at a wrong date.  And even if
multitasking is rare, in a multitasking environment havoc may result
for the other tasks. 

The wares (/pc/ts/tspas22.arc) are available by anonymous ftp from
chyde.uwasa.fi, Vaasa, Finland, 128.214.12.3, or by using our mail
server (use the latter if, and only if you don't have anonymous
ftp).  If you are not familiar with anonymous ftp or mail servers, I
am prepared send prerecorded instructions on request (provided that
your email address is reachable). 

...................................................................
Prof. Timo Salmi        (Moderating at anon. ftp site 128.214.12.3)
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun

g_harrison@vger.nsu.edu (10/11/90)

In article <1990Oct10.175221.17285@uwasa.fi>, ts@uwasa.fi (Timo Salmi) writes:
> In article <3334@vela.acs.oakland.edu> moconnor@argo.acs.oakland.edu writes:
>>Could someone please mail me code such that given a date (something
>>like, say, October 10th, 1990), it tells me the day of the week?
........
>  A1: There is a wkdayfn function in /pc/ts/tspas22.arc Turbo Pascal
> units collection to give the modern weekday based on Zeller's
> congruence. Also you can find a more extensive Julian and Gregorian
> weekday algorithm in Dr.Dobbs Journal, June 1989, p. 148. Furthermore
> Press & Flannery & al, Numerical Recipes, has the weekday code.
> 
>  A2: Some will recommend the following kludge.  Store the current
>  .....etc......
> 
> The wares (/pc/ts/tspas22.arc) are available by anonymous ftp from
> chyde.uwasa.fi, Vaasa, Finland, 128.214.12.3, or by using our mail
>  .....etc......
> Prof. Timo Salmi        (Moderating at anon. ftp site 128.214.12.3)
> School of Business Studies, University of Vaasa, SF-65101, Finland
> Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun

I sent the following Pascal function to the original poster.  It's very nice
and VERY fast.  I found it in "Computer Language Magazine" a few years ago:

....................................................................
	MONTHTYPE = 1..12;	{January..December}
	DAYTYPE   = 1..7;	{Sunday..Saturday}

{returns the first day of the MONTH in YEAR}
function FIRST_DAY(MONTH : MONTHTYPE; YEAR  : INTEGER) : DAYTYPE; 
var
    	DAY, MON, YEA : INTEGER; 
begin
	YEA := YEAR;
    	MON := MONTH;

    	if MON <= 2 then 
	begin
      		MON := MON + 12; 
      		YEA := YEA - 1
	end;

    	DAY := (3 + (2*MON) + (((MON + 1)*6) div 10) + YEA + (YEA div 4) - 
		(YEA div 100) + (YEA div 400)) mod 7; 

    	if DAY = 0 then 
      		FIRST_DAY := 7
    	else 
      		FIRST_DAY := DAY
end;

--------------------------------------------------------------------------
-- George C. Harrison                 INTERNET: g_harrison@vger.nsu.EDU --
-- Professor of Computer Science               Subliminal Address:  adA --
-- Norfolk State University                                             --
--   The views expressed here are not necessarily those of              --
--   the Univesity, my wife, or my children.                            --
--------------------------------------------------------------------------