[comp.binaries.ibm.pc.d] Looking for D.O.W. algorithm

chrisb@hubcap.clemson.edu (Chris) (09/15/89)

Hi, 
  I am looking for an algorithm which gives the day of the week (M,T,W,Th,...)
for a given date... (9,15,1989) Sept 15, 1989 --- would be Friday. 
Or else, I will settle for a way to tell what day of the week a given 
year begins on. I am having a little trouble coming up with these, and
thought someone out there may be able to lend a poor programmer a hand.
I thank you for your time. 

ChrisB@Hubcap.Clemson.Edu

sig ?? Nah...

brent@sactoh0.UUCP (Brent K. Barrett) (09/17/89)

In article <6470@hubcap.clemson.edu>, chrisb@hubcap.clemson.edu (Chris) writes:
> Hi, 
>   I am looking for an algorithm which gives the day of the week (M,T,W,Th,...)
> for a given date... (9,15,1989) Sept 15, 1989 --- would be Friday. 

 It's a BIOS call.  Here's a simple C function that returns the
D.O.W. (0=Sunday, 1=Monday, ... etc.):

int dow()
{
    _AX = 0x2A00;
    geninterrupt(0x21);
    return(_AL);
}
 

-- 
  ////////  Novucivitas: The Future of Citadel     
 ///         US 916 726 4989   12/2400 bps   
//////// ..ames!pacbell!sactoh0!brent   GEMAIL: B.K.BARRETT

ts@chyde.uwasa.fi (Timo Salmi LASK) (09/17/89)

In article <1839@sactoh0.UUCP> brent@sactoh0.UUCP (Brent K. Barrett) writes:
>In article <6470@hubcap.clemson.edu>, chrisb@hubcap.clemson.edu (Chris) writes:
>>   I am looking for an algorithm which gives the day of the week (M,T,W,Th,...)
>> for a given date... (9,15,1989) Sept 15, 1989 --- would be Friday. 

> It's a BIOS call.  Here's a simple C function that returns the

This is a subject that seems to pop up about every weekday:-)  The
bios call only returns the *current* weekday, but for *any* weekday
an algorithm is needed.  For that scan the old news eg in
comp.lang.pascal, or see Dr.Dobbs Journal, June 1989, page 148. 

...................................................................
Prof. Timo Salmi                                (Site 128.214.12.3)
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: vakk::salmi Bitnet: salmi@finfun

hughes@math.berkeley.edu (Eric Hughes) (09/18/89)

In article <766@chyde.uwasa.fi>, ts@chyde (Timo Salmi LASK) writes:
>This is a subject that seems to pop up about every weekday:-)  The
>bios call only returns the *current* weekday, but for *any* weekday
>an algorithm is needed.  

The standard trick under MSDOS is to save the current date, then set
the date to the desired one, get the day of the week, and finally
reset the date to the real one.

Alternatively, use a julian date routine, divide the resulting number
by seven, and use the remainder to get the day of the week; this
requires calibration to a known date-day pair.

Oh, and if you want to do this for dates previous to the 15th century,
you'll need to take into account various calendar adjustments that
were made. :-)

Eric Hughes
hughes@math.berkeley.edu   ucbvax!math!hughes

fisher@sc2a.unige.ch (Markus Fischer) (09/19/89)

In article <6470@hubcap.clemson.edu>, chrisb@hubcap.clemson.edu (Chris) writes:
>   I am looking for an algorithm which gives the day of the week (M,T,W,Th,...)
> for a given date... (9,15,1989) Sept 15, 1989 --- would be Friday. 

Here you go:

The method is simple: for jan. and feb., add 12 to `month' and substract 1 to
`year' (in fact, the year was always meant to start with march ! ever noticed
that sept. is 7, oct. 8, nov. 9, and dec. is 10 [as in decimal] ? :-).
Then take year*365.25 + (month+1)*30.6 + day, this gives you an arbitrary
`number of days'.
As this works only when even years come every four years, the range is
1.3.1900 - 28.2.2100.  So the best is to substract the value you'd get for the
fist sunday after the (non-existing) 29.2.1900, namely the 4.3.1900.

Easy!

Ah yes, the number modulus 7 gives you the day of the week (0 for sundays).

Finally, here's some C-code as an example:

--- file day.c ---
#include <stdio.h>
#include <stdlib.h>

long
days (int d, int m, int y)
/*
 * This function returns the number of days since the March 4th, 1900.
 *
 * Parameters: 3 ints representing the day, month and year.
 *             no checking is done: any int is accepted !
 * Range:      4.03.1989 - 27.2.2100 (there will be no 28.2.2100)
 *             For a longer range add the usual tests for missing even years.
 */
{
	return (
		+ (long)((float)(y-(m<3)) * 365.25)  /* y->d (-1 for jan. and feb.) */
		+ (long)((float)((m+9)%12+4) * 30.6) /* m->d (+12 "   "    "   "  ) */
		+ (long)d                            /* add d :-) */
		- (long)694101                       /* val of 4.03.1989 (a sunday) */
	);
}

main (int argc, char** argv)
{
	int day, month, year;
	long n;
	char *dow[7] = {"sunday","monday","tuesday","wednesday",
	                "thursday","friday","saturday"};

	if (argc<4)
		printf("Enter 3 ints as parameter \n");
	else {
		day = atoi(argv[1]);
		month = atoi(argv[2]);
		year = atoi(argv[3]);
		n = days (day, month, year);
		printf (" On %s %d.%02d.%04d, %ld days have elapsed since"
		        " sunday 4.03.1989.\n",
		        dow[(unsigned)n%7], day,month,year, n);
	}
}
--- EOF ---

Hope this helps !

Markus Fischer                -|--|--|--|--|--|--I   Department of Anthropology
                        -|--|--|--|--|--|--|-(#)-I   University of Geneva
      -|--|--|--|--|--|--|--|--|-(#)-|-(#)(#)(_)-I   CH-1227  Carouge (GE)
   -&-(_)-|--|--|-(#)-&--|-(#)(#)(_)(#)-&-(_)(#)-I   Switzerland
            -|--|--|--|--|-(#)(_)-|-(_)(_)(_)(#)-I
black (#) to kill ! --|--|-(#)(_)(_)(_)(#)(#)(_)(_)  fisher@sc2a.unige.ch
=+==+==+==+==+==+==+==+==+==+==+==+==+==+==+=(#)=+   fisher@cgeuge52.bitnet

ralf@b.gp.cs.cmu.edu (Ralf Brown) (09/19/89)

In article <1989Sep18.165034.19753@agate.berkeley.edu> hughes@math.berkeley.edu (Eric Hughes) writes:
}>bios call only returns the *current* weekday, but for *any* weekday
}>an algorithm is needed.  
}
}The standard trick under MSDOS is to save the current date, then set
}the date to the desired one, get the day of the week, and finally
}reset the date to the real one.

Which of course is not too nice if you're running a multitasker and another
task asks for the date in the middle of the above process....

}Oh, and if you want to do this for dates previous to the 15th century,
}you'll need to take into account various calendar adjustments that
}were made. :-)

Don't forget to include a database of which adjustments were made when by
which countries (Russia didn't switch from Julian until early this century,
England and the at-that-time American colonies switched in 1752).

-- 
{backbone}!cs.cmu.edu!ralf   ARPA: RALF@CS.CMU.EDU   FIDO: Ralf Brown 1:129/46
BITnet: RALF%CS.CMU.EDU@CMUCCVMA   AT&Tnet: (412)268-3053 (school)   FAX: ask
DISCLAIMER? |"Humor is laughing at what you haven't got when you ought to
What's that?| have it."  -- Langston Hughes

fer@phyllis.math.binghamton.edu (fernando guzman) (09/30/89)

>Here you go:
>
>The method is simple: for jan. and feb., add 12 to `month' and substract 1 to
>`year' (in fact, the year was always meant to start with march ! ever noticed
>that sept. is 7, oct. 8, nov. 9, and dec. is 10 [as in decimal] ? :-).

September became the 9th month, october the 10th, and so on, when the Roman
Emperors Julius Caesar and Augustus Caesar decided to modify the calendar and
create months in their own honor. )-: