Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!ut-sally!pyramid!ncr-sd!greg From: greg@ncr-sd.UUCP (Greg Noel) Newsgroups: net.lang.c Subject: Re: Calendar Functions Message-ID: <1165@ncr-sd.UUCP> Date: Thu, 11-Sep-86 18:27:20 EDT Article-I.D.: ncr-sd.1165 Posted: Thu Sep 11 18:27:20 1986 Date-Received: Thu, 11-Sep-86 22:46:36 EDT References: <206@cascade.STANFORD.EDU> <1229@loral.UUCP> Reply-To: greg@ncr-sd.UUCP (Greg Noel) Organization: NCR Corporation, San Diego Lines: 31 Keywords: Calendars In article <1229@loral.UUCP> dml@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@ncr-sd.UUCP or Greg@nosc.ARPA