Article View: net.lang.c
Article #9306Re: Calendar Functions (simpler leap year calculation)
From: Bob Devine
Date: Fri, 12 Sep 1986 20:33
Date: Fri, 12 Sep 1986 20:33
28 lines
1226 bytes
1226 bytes
Dave Lewis writes:
> leapyear (year)
> int year;
> {
> if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
> return (1);
> else
> return (0);
> }
While this works, it is overkill. Unless you believe that your
code will make it to the year 2100, a simple test for divisibility
by 4 is sufficient. If you really want an algorithm for all years,
you then need to also test for years divisible by 4000...
int
leapyear(year)
int year;
{
/* works for the years 1901-2099 */
return(year%4);
}
Or if you want a simple cpp macro:
#define isleapyear(year) ((year)%4)
Bob Devine
Technical Details
Message-ID:
<34@vianet.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!mnetor!seismo!lll-crg!lll-lcc!styx!twg-ap!amdahl!hplabs!hao!nbires!vianet!devineReferences:
<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!lll-crg!lll-lcc!styx!twg-ap!amdahl!hplabs!hao!nbires!vianet!devine From: devine@vianet.UUCP (Bob Devine) Newsgroups: net.lang.c Subject: Re: Calendar Functions (simpler leap year calculation) Message-ID: <34@vianet.UUCP> Date: Fri, 12-Sep-86 16:33:30 EDT Article-I.D.: vianet.34 Posted: Fri Sep 12 16:33:30 1986 Date-Received: Sun, 14-Sep-86 03:48:28 EDT References: <206@cascade.STANFORD.EDU> <1229@loral.UUCP> Distribution: net.lang.c Organization: ViaNetix, Inc., Boulder, CO Lines: 28