Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!usc!rutgers!dimacs.rutgers.edu!seismo!beno!black From: black@beno.CSS.GOV (Mike Black) Newsgroups: comp.lang.c Subject: Re: day of week Summary: source code for day of week Message-ID: <49118@seismo.CSS.GOV> Date: 22 Sep 90 17:34:10 GMT References: <1990Sep21.232813.2516@cbnewsm.att.com> Sender: usenet@seismo.CSS.GOV Distribution: usa Organization: Center for Seismic Studies, Arlington, VA Lines: 90 In article <1990Sep21.232813.2516@cbnewsm.att.com> sfs@cbnewsm.att.com (salvatore.savastano) writes: > >I have a function that works as follows: > If the parameter passed is zero it returns the current > time in UNIX long as in time(2); > otherwise the value passed is considered to be in the format > YYMMDD and returns the number of days since Jan 1, 1970 > >using this as a basis, does anyone have a program or an >algorithm that for a parameter also specified as a date in >YYMMDD format, will return the day of the week on which that date >orrurred? Someone posted the algorithm earlier and I made the following code which you should be able to easily change (i.e. prograis standalone with dotw function built in): /* I made the code to go with the following algorithm. Makes for a simple enough routine. Mike Black, black@beno.CSS.GOV, Melbourne FL. 10 Sep 90 */ /* Here is an algorithm known as Zeller's congruence. It may be of general interest, so I am posting it. I found it in a magazine (I forget which one) a few years ago. The day of the week (0 = Sunday) is given by W = ((26*M - 2) / 10 + D + Y + Y/4 + C/4 - 2*C) % 7 where C is the century Y is the last two digits of the year D is the day of the month M is a special month number, where Jan and Feb are taken as month 11 and 12 of the previous year Each division is truncating division, and the terms cannot be combined. -- Steve Clamage, TauMetric Corp, steve@taumet.com */ int dotw(day,month,year,century) int day,month,year,century; { if(month==1) { month=11; year--; } else if (month==2) { month=12; year--; } else month-=2; return ((26*month - 2) / 10 + day + year + year/4 + century/4 - 2 * century ) % 7; } char *daystr[7] = { "Sun","Mon","Tues","Wednes","Thurs","Fri","Satur" }; main(argc,argv) int argc; char **argv[]; { int month,day,century,year,n; if ((n=sscanf(argv[1],"%d/%d/%2d%2d",&month,&day,¢ury,&year)) != 4) { if (n == 3 && century>=90 && century<=99) { /* allow for 1990-1999 */ year = century; century = 19; } else { puts("Usage: dotw month/day/year, i.e. 9/10/1990"); puts("1990-1999 can be abbreviated as 90-99"); exit(5); } } n = dotw(day,month,year,century); printf("%d/%d/%2d%2d is a %sday\n",month,day,century,year, daystr[n]); return n; } -- ------------------------------------------------------------------------------- : usenet: black@beno.CSS.GOV : land line: 407-494-5853 : I want a computer: : real home: Melbourne, FL : home line: 407-242-8619 : that does it all!: -------------------------------------------------------------------------------