Calendar Functions
Greg Noel
greg at ncr-sd.UUCP
Fri Sep 12 08:27:20 AEST 1986
In article <1229 at loral.UUCP> dml at loral.UUCP (Dave Lewis) writes:
>/* Given a valid date (month, day, and year) Zeller will
> * return an integer representing the day of week that
> * date will fall on. 0 = Sunday, 6 = Saturday.
> */
>
>zeller (month, day, year)
>int month, day, year;
>{
> [ various code deleted..... ]
> return (((2.6 * month - 0.1) + day + year + year / 4
> + century / 4 - century * 2) % 7);
>}
Note that the expression above is in floating point. This may produce bogus
results if the divisions (which are supposed to be truncated) end up being
done in floating point (they \shouldn't/ be, but....). Certainly, converting
all those ints to floats to be added and converted back to an int will take
a lot of time. Also, it is possible that the major expression (prior to the
modulo operation) can be negative, so it is best to add in a bias value that
is zero mod seven to be sure that the calculated modulus is also positive.
Thus, a logically equivalent version, done with all integer arithmetic is:
return ((26 * month - 1)/10 + day + year + year/4
+ century/4 - century*2 + 777) % 7;
BTW, the reason it's called a congruence is that with this formulation it's
easy to demonstrate that the the day-of-the-year pattern repeats every three
hundred years.
--
-- Greg Noel, NCR Rancho Bernardo Greg at ncr-sd.UUCP or Greg at nosc.ARPA
More information about the Comp.lang.c
mailing list