Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!mcsun!cernvax!cui!ugun2b!ugsc2a!fisher From: fisher@sc2a.unige.ch (Markus Fischer) Newsgroups: comp.binaries.ibm.pc.d Subject: Re: Looking for D.O.W. algorithm Message-ID: <125@sc2a.unige.ch> Date: 18 Sep 89 17:37:38 GMT References: <6470@hubcap.clemson.edu> Organization: University of Geneva, Switzerland Lines: 74 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 #include 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