🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.lang.c
Article #9339

Re: Calendar Functions

#9339
From: franka
Date: Mon, 15 Sep 1986 18:38
37 lines
1799 bytes
In article <1229@loral.UUCP> dml@loral.UUCP (Dave Lewis) writes:
>daysinmonth (month, year)
>int month, year;
>{
>  if (month == 2)          /* Is it February?             */
>     if (leapyear (year))  /* If so, is it a leap year?   */
>	return (29);       /* 29 days in Feb in leap year */
>     else
>	return (28);       /* 28 days if not              */
>  else{
>     if (month > 7)        /* Is it August -> December?   */
>	month++;           /* Invert even/odd state if so */
>     if (month & 1)        /* Odd months have 31 days     */
>	return (31);
>     else
>	return (30);       /* Even months have 30 days    */
>  }
>}

This is ok, but it is easier to use a table:

daysinmonth(month, year)
int month, year)
{
   int monthlengths[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

   if (month == 2 && leapyear(year)) {
      return (29);
   } else {
      return monthlengths[month - 1];
   }
}

(Comments are left as an exercise for the reader.)

Frank Adams                           ihnp4!philabs!pwa-b!mmintl!franka
Multimate International    52 Oakland Ave North    E. Hartford, CT 06108
Technical Details
Message-ID: <1805@mmintl.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!ut-sally!husc6!panda!genrad!decvax!decwrl!amdcad!amdimage!prls!philabs!pwa-b!mmintl!franka
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!husc6!panda!genrad!decvax!decwrl!amdcad!amdimage!prls!philabs!pwa-b!mmintl!franka
From: franka@mmintl.UUCP
Newsgroups: net.lang.c
Subject: Re: Calendar Functions
Message-ID: <1805@mmintl.UUCP>
Date: Mon, 15-Sep-86 14:38:56 EDT
Article-I.D.: mmintl.1805
Posted: Mon Sep 15 14:38:56 1986
Date-Received: Sat, 20-Sep-86 01:02:16 EDT
References: <206@cascade.STANFORD.EDU> <1229@loral.UUCP>
Reply-To: franka@mmintl.UUCP (Frank Adams)
Distribution: net.lang.c
Organization: Multimate International, E. Hartford, CT
Lines: 37
Keywords: Calendars