🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.lang.c
Article #9296

Re: Calendar Functions

#9296
From: Greg Noel
Date: Thu, 11 Sep 1986 22:27
31 lines
1953 bytes
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
Technical Details
Message-ID: <1165@ncr-sd.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!ut-sally!pyramid!ncr-sd!greg
References: <206@cascade.STANFORD.EDU> <1229@loral.UUCP>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!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