[comp.sys.ibm.pc] day of week

fubar@squid.UUCP (01/28/88)

From squid!fubar Thu Jan 28 04:22 CDT 1988 remote from occrsh
Subject: day of week

***
*** PLEASE send replies to ihnp4!occrsh!squid!david
***

>   Here's a formula given in the book An Introduction to the Theory of Numbers,
>by Niven and Zuckerman.
>
>   Let M be the number of the month, defined so that Jan = 11, Feb = 12,
>Mar = 1, Apr = 2, ..., Dec = 10.  Let N be the number of the day within the
>month.  Let C be the hundreds in the year, and let Y be the rest of the year.
>Let L be 1 for a leap year and 0 for a non-leap year.
>
>The formula
>
>(N + [2.6M - 0.2] + Y + [Y / 4] + [C / 4] - 2C - (1 + L)[M / 11]) mod 7
>yields 0 for Sunday, 1 for Monday, ..., 6 for Saturday.
>
>(Square brackets denote the greatest integer function.)

So:

Thursday, Jan 28, 1988, should yield 4:

 N = 28   (number of day within month)
 M = 11   (Jan = 11)
 C = 19   (Hundreds w/in year)
 Y = 88   (rest of year??)
 L = 1    (1988 is leap year)

 = (N  +  [2.6M - 0.2]  +  Y +  [Y/4] +  [C/4] -   2C   -  (1+L)[M/11])  mod 7
 = (28 + [(2.6*11)-0.2] + 88 + [88/4] + [19/4] - (2*19) - (1+1)*[11/11]) mod 7
 = (28 +     [28.4]     + 88 +  [22]  + [4.75] -   38   -      2)        mod 7
 = (28 +       29       + 88 +   22   +   5    -   38   -      2)        mod 7
 = 132 mod 7
 = 6 ??

What is the correct formula for this?

David Drexler
 uucp:       {ihnp4,cbosgd,moss,uokmax}!occrsh!squid!david
 fidonet:    147/1  -or-  19/1
 sneakernet: POB 1214, Bethany OK 73008
 MaBellNet:  [405] 848-8868 (voice)
             [405] 728-2463 (data - bbs)

wnp@killer.UUCP (Wolf Paul) (02/01/88)

In article <141900013@occrsh.ATT.COM> fubar@squid.UUCP writes:
]
]From squid!fubar Thu Jan 28 04:22 CDT 1988 remote from occrsh
]Subject: day of week
]
]***
]*** PLEASE send replies to ihnp4!occrsh!squid!david
]***
]
]>(Square brackets denote the greatest integer function.)
]So:
]Thursday, Jan 28, 1988, should yield 4:
] N = 28   (number of day within month)
] M = 11   (Jan = 11)
] C = 19   (Hundreds w/in year)
] Y = 88   (rest of year??)
] L = 1    (1988 is leap year)
] = (N  +  [2.6M - 0.2]  +  Y +  [Y/4] +  [C/4] -   2C   -  (1+L)[M/11])  mod 7
] = (28 + [(2.6*11)-0.2] + 88 + [88/4] + [19/4] - (2*19) - (1+1)*[11/11]) mod 7
] = (28 +     [28.4]     + 88 +  [22]  + [4.75] -   38   -      2)        mod 7
] = (28 +       29       + 88 +   22   +   5    -   38   -      2)        mod 7
] = 132 mod 7
] = 6 ??
]
]What is the correct formula for this?

Seems to me the formula is ok, but your calculations are not. The greatest
integer in 28.4 is 28, not 29; in 4.75 it is 4, not 5.

Thus, the last few lines of your calculation above should read:

  = (28 +       28       + 88 +   22   +   4    -   38   -      2)        mod 7
  = 130 mod 7
  = 4

"greatest integer" is not the same as rounding up to the next highest integer
:-) ...


Wolf Paul
ihnp4!killer!wnp

julian@uhccux.UUCP (Julian Cowley) (02/02/88)

Here is a formula that I picked out of an old HP-2000 calendar program.
Basically it figures out how many days there are since January 1, 1900.
As that date was a Monday, the result modulo 7 starts on that day of
the week.  This should work for all of the years in this century, with
the exception of 1900 because that year was not a leap year.

Sample C program:

char *daystr[7] = {
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thursday",
  "Friday",
  "Saturday",
  "Sunday" };

main()
{
  int day, year, month, date;

  puts("Enter the date as mm/dd/yy");
  scanf("%d/%d/%d", &month, &date, &year);

  day = ((year-1)*365 + (year-1)/4 + (int) ((month-1)*30.57+0.5) -
        (1+(year%4 != 0))*(month > 2) + date) % 7;

  printf("That day was a %s\n", daystr[day]);
}

Cheers.

 //////  /////  Julian Cowley, University of Hawaii  | "It's not the size of
    // //   //  ...!ihnp4!sdcsvax!nosc!uhccux!julian |  the disk that matters,
\\ // //        julian@uhccux.uhcc.hawaii.edu	     |  it's the way it's
 \X/   /////    julian@uhccux.bitnet   	  	     |  formatted..."

davidsen@steinmetz.steinmetz.UUCP (William E. Davidsen Jr) (02/03/88)

There has been so much commentary on the topic of "what day of the
week," that I am posting a short program to provide the information. It
returns a pseudo-julian date which may be used to uniquely identify any
date from Jan 1 1980 to Feb 29, 2100. One procedure takes the month,
day, year, and returns a unique number, the other does the inverse. The
number is such that num%7 = day_of_week, and 0 means Sunday.

Before I get a million postings, yes I have a real julian date routine.
This one works and returns values which will fit in a short as long as I
live.

/*
 *  JULI - pseudo julian date package
 *
 *  Convert any post-1980 date to an integer day number and back to
 *  day, month, year. This will work up to feb 29, 2100.
 */

static months[12][2] = {
      {0, 0}, {31, 31}, {59, 60}, {90, 91},
      {120, 121}, {151, 152}, {181, 182}, {212, 213},
      {243, 244}, {273, 274}, {304, 305}, {334, 335}};

int jdate (month, day, year)
    int month, day, year;
{
    register int iy, ly, lyf;

    iy = year - 1980;
    ly = (iy+3) / 4; /* leap year count */
    lyf = (iy%4 == 0);
    return (365*iy + ly + months[month-1][lyf] + day);
}

/*
 *  Convert day number to date
 */

unjuli (jd, month, day, year)
    int jd; /* julian day */
    int *month, *day, *year;
{
    register int
    lyf, /* leap year (this year) flag */
    dd, mm, /* calculated day and month */
    yy, ly; /* incremental years, leapyears */

  /* first guess as to year */
    yy = jd / 365;
    ly = (yy+3) / 4;
    while ((dd = jd - 365*yy - ly) <= 0) {
    /* correct for dates near the end of a year */
      yy--;
      ly = (yy+3) / 4;
    }

  /* find the month */
    lyf = (yy%4 == 0);
    mm = 0;
    while (months[mm][lyf] < dd && ++mm < 12);

  /* return values */
    *year = yy + 1980;
    *month = mm;
    *day = dd - months[mm-1][lyf];
}

/*
 *  Test routine for julian package
 */

#ifdef TEST
#include <stdio.h>

main () {
    int m, d, y, jd;

    for ( ; ; ) {
      printf ("Enter month, day, year: ");
      scanf ("%d%d%d", &m, &d, &y);
      if (y < 1900) y += 1900;
      jd = jdate (m, d, y);
      printf ("Juliam for %d/%d/%d = %d\n", m, d, y, jd);

      unjuli (jd, &m, &d, &y);
      printf ("Converted back = %d/%d/%d\n", m, d, y);
    }
}
#endif

-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

hirayama@suvax1.UUCP (Pat Hirayama) (02/04/88)

In article <141900013@occrsh.ATT.COM>, fubar@squid.UUCP writes:
> ***
> *** PLEASE send replies to ihnp4!occrsh!squid!david
> 
> >   Here's a formula given in the book An Introduction to the Theory of Numbers,
> >by Niven and Zuckerman.
> >   Let M be the number of the month, defined so that Jan = 11, Feb = 12,
> >Mar = 1, Apr = 2, ..., Dec = 10.  Let N be the number of the day within the
> >month.  Let C be the hundreds in the year, and let Y be the rest of the year.
> >Let L be 1 for a leap year and 0 for a non-leap year.
> >
> >
> >(N + [2.6M - 0.2] + Y + [Y / 4] + [C / 4] - 2C - (1 + L)[M / 11]) mod 7
> >yields 0 for Sunday, 1 for Monday, ..., 6 for Saturday.
> >
> >(Square brackets denote the greatest integer function.)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> Thursday, Jan 28, 1988, should yield 4:
> 
>  N = 28   (number of day within month)
>  M = 11   (Jan = 11)
>  C = 19   (Hundreds w/in year)
>  Y = 88   (rest of year??)
>  L = 1    (1988 is leap year)
> 
>  = (N  + [2.6M - 0.2]  +  Y +  [Y/4] +  [C/4] -   2C   -  (1+L)[M/11])  mod 7
>  = (28 + (2.6*11)-0.2] + 88 + [88/4] + [19/4] - (2*19) - (1+1)*[11/11]) mod 7
>  = (28 +    [28.4]     + 88 +  [22]  + [4.75] -   38   -      2)        mod 7
>  = (28 +      29       + 88 +   22   +   5    -   38   -      2)        mod 7
>  = 6 ??
> 
> What is the correct formula for this?

David,

You are ignoring the underlined statement indicated above.  If you consider
this, you will realize the following:

Given, your initial data values given above.

Let X=Number you are seeking...

 
X  = (N  + [2.6M - 0.2]  +  Y +  [Y/4] +  [C/4] -   2C   -  (1+L)[M/11])  mod 7
   = (28 + (2.6*11)-0.2] + 88 + [88/4] + [19/4] - (2*19) - (1+1)*[11/11]) mod 7
   = (28 +    [28.4]     + 88 +  [22]  + [4.75] -   38   -      2)        mod 7
   = (28 +      28   	 + 88 +   22   +   4  	-   38	 -	2)	  mod 7
   = 4

Which is Thursday and the answer that you expect to get.


------------------------------------------------------------------------------
 Pat Hirayama		UUCP:    ...!uw-entropy!dataio!suvax1!hirayama
 Seattle University	USNail:  28625-47th Pl S, Auburn, WA 98001-1140
 Class of 1988		Phone:	 (206) 946-0833

 ". . . the starships of the Federation are the physical, tangible
  manifestations of humanity's stubborn insistence that life does indeed
  mean something."
				Spock
				Final Frontier
------------------------------------------------------------------------------