linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* A Calendar app on C
@ 2004-06-02  5:58 Fabio Miranda Hamburger
  2004-06-02  7:10 ` Christian Beckel
  2004-06-02 10:07 ` Glynn Clements
  0 siblings, 2 replies; 6+ messages in thread
From: Fabio Miranda Hamburger @ 2004-06-02  5:58 UTC (permalink / raw)
  To: linux-c-programming

Hi.

I am trying to program a basic calendar software in C. The idea is to ask
for a year and display all the calendar from january to december. How to
code such app taking care of 31/28 days month and check each 4 year for a
leap year? Any idea/comment/advice/code is welcome.


best regards,

---
Fabio Andres Miranda
Ingenieria de sistemas informaticos
Universidad Latina - Costa Rica


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: A Calendar app on C
  2004-06-02  5:58 A Calendar app on C Fabio Miranda Hamburger
@ 2004-06-02  7:10 ` Christian Beckel
  2004-06-02  7:17   ` Christian Beckel
  2004-06-02 10:07 ` Glynn Clements
  1 sibling, 1 reply; 6+ messages in thread
From: Christian Beckel @ 2004-06-02  7:10 UTC (permalink / raw)
  To: linux-c-programming

Hi,

there are several possibilities to compute the number of days in a month or if 
there is a leap or not.

if (i == 2) 
	days = 28;
else
	days = 31 - ( (i != 4 || i != 6 || i != 9 || i != 11) ? 1 : 0 );

I think there is no simpler way, because the numbers of months are unregular.



> Hi.
>
> I am trying to program a basic calendar software in C. The idea is to ask
> for a year and display all the calendar from january to december. How to
> code such app taking care of 31/28 days month and check each 4 year for a
> leap year? Any idea/comment/advice/code is welcome.
>
>
> best regards,
>
> ---
> Fabio Andres Miranda
> Ingenieria de sistemas informaticos
> Universidad Latina - Costa Rica
>
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: A Calendar app on C
  2004-06-02  7:10 ` Christian Beckel
@ 2004-06-02  7:17   ` Christian Beckel
  2004-06-02  9:18     ` John T. Williams
  0 siblings, 1 reply; 6+ messages in thread
From: Christian Beckel @ 2004-06-02  7:17 UTC (permalink / raw)
  To: linux-c-programming

oops, I forgot to test if there is a leap or not, the first line should be:

if (i == 2)
	days = (i % 4 != 0) ? 28 : 29;


> Hi,
>
> there are several possibilities to compute the number of days in a month or
> if there is a leap or not.
>
> if (i == 2)
> 	days = 28;
> else
> 	days = 31 - ( (i != 4 || i != 6 || i != 9 || i != 11) ? 1 : 0 );
>
> I think there is no simpler way, because the numbers of months are
> unregular.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: A Calendar app on C
  2004-06-02  7:17   ` Christian Beckel
@ 2004-06-02  9:18     ` John T. Williams
  2004-06-02 10:38       ` wwp
  0 siblings, 1 reply; 6+ messages in thread
From: John T. Williams @ 2004-06-02  9:18 UTC (permalink / raw)
  To: linux-c-programming

isn't there some rule about if the year is year%100=0  then is has to
also be yea%400 to be a leap year?    



On Wed, 2004-06-02 at 03:17, Christian Beckel wrote:
> oops, I forgot to test if there is a leap or not, the first line should be:
> 
> if (i == 2)
> 	days = (i % 4 != 0) ? 28 : 29;
> 
> 
> > Hi,
> >
> > there are several possibilities to compute the number of days in a month or
> > if there is a leap or not.
> >
> > if (i == 2)
> > 	days = 28;
> > else
> > 	days = 31 - ( (i != 4 || i != 6 || i != 9 || i != 11) ? 1 : 0 );
> >
> > I think there is no simpler way, because the numbers of months are
> > unregular.
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: A Calendar app on C
  2004-06-02  5:58 A Calendar app on C Fabio Miranda Hamburger
  2004-06-02  7:10 ` Christian Beckel
@ 2004-06-02 10:07 ` Glynn Clements
  1 sibling, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2004-06-02 10:07 UTC (permalink / raw)
  To: Fabio Miranda Hamburger; +Cc: linux-c-programming


Fabio Miranda Hamburger wrote:

> I am trying to program a basic calendar software in C. The idea is to ask
> for a year and display all the calendar from january to december. How to
> code such app taking care of 31/28 days month and check each 4 year for a
> leap year? Any idea/comment/advice/code is welcome.

A year is a leap year if:

	static int is_leap(int year)
	{
		return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
	}

Thus:

	static int days_in_year(int year)
	{
		return is_leap(year) ? 366 : 365;
	}

So the number of days in a month is:

	static int days_in_month(int year, int month)
	{
		static const int days[2][12] = {
			{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
			{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
		};

		return days[is_leap(year)][month-1];
	}

As for the relationship between days of the year and the days of the
week, note that the 400-year cycle for leap years is an exact number
of weeks (400 years == 400 * 365 + 97 == 146097 days == 20871 * 7
days), and that 2000-01-01 was a Saturday, so:

	int year_start(int year)
	{
		int day = 6;	/* Saturday */
		int i;

		year %= 400;

		for (i = 0; i < year; i++)
			day += days_in_year(i);

		return day % 7;
	}

This could easily be optimised (e.g. using a lookup table), but the
above is all of the information required to produce a calendar for any
given year.

[Unless, that is, you want it to work for years prior to the adoption
of the Gregorian calendar; note the output from "cal 1752".]

-- 
Glynn Clements <glynn.clements@virgin.net>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: A Calendar app on C
  2004-06-02  9:18     ` John T. Williams
@ 2004-06-02 10:38       ` wwp
  0 siblings, 0 replies; 6+ messages in thread
From: wwp @ 2004-06-02 10:38 UTC (permalink / raw)
  To: linux-c-programming

Hello John,


On Wed, 02 Jun 2004 05:18:57 -0400 "John T. Williams" <jowillia@vt.edu> wrote:

> isn't there some rule about if the year is year%100=0  then is has to
> also be yea%400 to be a leap year?   

Yes there is! Google would report many pages around this.
According to http://www.mitre.org/tech/cots/LEAPCALC.html:

             if (year mod 4 != 0)
                 {use 28 for days in February}
             else if  (year mod 400 == 0)
                 {use 29 for days in February}
             else if (year mod 100 == 0)
                 {use 28 for days in February}
             else
                 {use 29 for days in February}


Regards,

> On Wed, 2004-06-02 at 03:17, Christian Beckel wrote:
> > oops, I forgot to test if there is a leap or not, the first line should be:
> > 
> > if (i == 2)
> > 	days = (i % 4 != 0) ? 28 : 29;
> > 
> > 
> > > Hi,
> > >
> > > there are several possibilities to compute the number of days in a month or
> > > if there is a leap or not.
> > >
> > > if (i == 2)
> > > 	days = 28;
> > > else
> > > 	days = 31 - ( (i != 4 || i != 6 || i != 9 || i != 11) ? 1 : 0 );
> > >
> > > I think there is no simpler way, because the numbers of months are
> > > unregular.
> > -
> > To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> > the body of a message to majordomo@vger.kernel.org
> > More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 


-- 
wwp

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2004-06-02 10:38 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-06-02  5:58 A Calendar app on C Fabio Miranda Hamburger
2004-06-02  7:10 ` Christian Beckel
2004-06-02  7:17   ` Christian Beckel
2004-06-02  9:18     ` John T. Williams
2004-06-02 10:38       ` wwp
2004-06-02 10:07 ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).