[comp.lang.c] Day of week algorithm wanted for "C"

lenny@icus.islp.ny.us (Lenny Tropiano) (11/20/89)

I need a function that accepts a month, a day, and a year ... and returns
the day-of-the-week (ie. Sunday, Monday, etc..)

Thanks,
Lenny
-- 
| Lenny Tropiano            ICUS Software Systems      [w] +1 (516) 589-7930 |
| lenny@icus.islp.ny.us     Telex; 154232428 ICUS      [h] +1 (516) 968-8576 |
| {ames,pacbell,decuac,hombre,sbcs,attctc}!icus!lenny     attmail!icus!lenny |
+------- ICUS Software Systems -- PO Box 1;  Islip Terrace, NY  11752 -------+

moorer@jacobs.CS.ORST.EDU (Rocky Moore) (11/20/89)

In article <1031@icus.islp.ny.us> lenny@icus.islp.ny.us (Lenny Tropiano) writes:
>I need a function that accepts a month, a day, and a year ... and returns
>the day-of-the-week (ie. Sunday, Monday, etc..)

Here is a little piece of code I pulled out of the time routine I use.  It will
return a value 0-6 where 0=Sunday, 1=Monday, ect...

--- CUT HERE ---

/* Find and return day of week (0-6) */

int day_of_week(int year, int month, int day)
{
     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);
}

--- END ---

This should handle what you want.  Hope it helps.

Rocky Moore     moorer@jacobs.cs.orst.edu

Bob.Stout@p6.f506.n106.z1.fidonet.org (Bob Stout) (11/22/89)

In an article of <20 Nov 89 02:02:37 GMT>, (Lenny Tropiano) writes:

 >I need a function that accepts a month, a day, and a year ... and returns
 >the day-of-the-week (ie. Sunday, Monday, etc..)
------------------------------- Cut Here -------------------------------------
/*
** scalar date routines    --    public domain by Ray Gardner
** These will work over the range 1/01/01 thru 14699/12/31
*/

int isleap (yr)
unsigned yr;
{
   return yr % 400 == 0 || (yr % 4 == 0 && yr % 100 != 0);
}

static unsigned months_to_days (month)
unsigned month;
{
   return (month * 3057 - 3007) / 100;
}

static long years_to_days (yr)
unsigned yr;
{
   return yr * 365L + yr / 4 - yr / 100 + yr / 400;
}

long ymd_to_scalar (yr, mo, day)
unsigned yr, mo, day;
{
   long scalar;
   scalar = day + months_to_days(mo);
   if ( mo > 2 )                         /* adjust if past February */
      scalar -= isleap(yr) ? 1 : 2;
   yr--;
   scalar += years_to_days(yr);
   return scalar;
}

void scalar_to_ymd (scalar, pyr, pmo, pday)
long scalar;
unsigned *pyr, *pmo, *pday;
{
   unsigned n;                /* compute inverse of years_to_days() */
   for ( n = (scalar * 400L) / 146097; years_to_days(n) < scalar; )
      n++;                          /* 146097 == years_to_days(400) */
   *pyr = n;
   n = scalar - years_to_days(n-1);
   if ( n > 59 ) {                       /* adjust if past February */
      n += 2;
      if ( isleap(*pyr) )
         n -= n > 62 ? 1 : 2;
   }
   *pmo = (n * 100 + 3007) / 3057;  /* inverse of months_to_days() */
   *pday = n - months_to_days(*pmo);
}

int scalar_to_day(scalar)
long scalar;
{
    return (int)(scalar % 7L);      /* 0 = Sunday, 1 = Monday, etc. */
}
------------------------------- Cut Here -------------------------------------