Date functionality

Moshe Raab raab at novavax.UUCP
Sat Aug 11 03:16:48 AEST 1990



In article <8931 at ubc-cs.UUCP> news at cs.ubc.ca writes:
>>Hello,
>>       I am looking for an algorithm to give me the day of the week when
>>handed the current date.  I can't use any builtin routines from any OS
>>(this is targeted for a real brain-damaged machine).  Any Help appreciated.
 
>  Answering this question was my first post, a few months ago.
>  Let's see if I can get it right this time.  The statement function
>  following will work:
>
>   DOW(ID,IM,IC,IY)=MOD((26*IM-2)/10+ID+IY+IY/4+IC/4-2*IC,7)
 
> where ID is the day of the month, IM is two less than the month
> from March on (March is 1, ..., December is 10) or 11 for January
>  or 12 for February, IC is the century and IY is the year.  The
>  value of DOW is minus one for Saturday, 0 for Sunday, ..., 5
>  for Friday.  At least, it is if I read the program comments
>  correctly this time.  It won't work before 1753.
 
this function will not work for the month of jan & feb unless you also
decrease the year, as the following code seg shows:
 
int dow(int id,int im,int ic,int iy)
{
/*
ID is the day of the month,
IM is two less than the month from March on (March is 1, ..., December is 10)
    or 11 for January or 12 for February,
IC is the century
IY is the year.
*/
  if (im > 2) im -= 2;
  else
  {
    im += 10;
    if (!iy)
    {
      ic--;
      iy = 99;
    }
    else iy--;
  }
  return ((26*im-2)/10+id+iy+iy/4+ic/4-2*ic) % 7;
}



More information about the Comp.lang.c mailing list