Computing Day of the Week
rick furniss
rickf at pmafire.UUCP
Thu Dec 7 03:07:32 AEST 1989
#include <stdio.h>
main (argc,argv)
char **argv;
int argc;
{
int result;
int year=atoi(argv[1]), month=atoi(argv[2]), day=atoi(argv[3]);
if (argc != 4 ) {
fprintf(stderr,"Usage: %s YY MM DD\n",argv[0]);
exit(1);
}
result=day_of_week(year,month,day);
fprintf(stdout,"day # %i\n",result);
exit(0);
}
/* Find and return day of week (0-6) */
int day_of_week(year,month,day)
int year,month,day;
{
static int offsets[13] = { 0,0,3,3,6,1,4,6,2,5,7,3,5 };
int dw;
dw=6+year+((year+3)/4)+offsets[month]+day;
if( ((year%4) ==0) && (month > 2)) dw++;
if( (year==0) && (month < 3)) dw++;
dw=(dw%7);
return(dw);
}
Standard disclaimer *******
Rick Furniss
More information about the Comp.lang.c
mailing list