views:

117

answers:

6

What will be the exact definition of leap year? AFAIK "A year which is divisible by 4 is a leap year. But for century years' the years which are divisible by 400 is a leap year."

But that definition makes 100, 200, 300, 400.... upto 1700 NOT LEAP years! But in Gregorian calendar all of them are all leap year, check this out.

You can also try "call 1700" in Linux to verify.

So the correct algorithm for leap years would be:

if ( (year % 4 == 0) && ( year <= 1700 || year % 100 != 0 || year % 400 == 0 ))
    printf("%d is a leap year.\n", year);
 else
    printf("%d is not a leap year.\n", year);

But is this specific to Gregorian calendar? Even if that is the case why is it not mentioned here?

Regards,

PS:The history of Gregorian callender seems interesting check out the September month of 1752.

A: 

Have a look here. I'll copy some of the pseudo-code here for reference:

if (year modulo 4 is 0) and ((year modulo 100 is not 0) or (year modulo 400 is 0))
       then is_leap_year
else
       not_leap_year
Bozhidar Batsov
Put parentheses around your and/or. As it stands, your statement is ambiguous. (gcc would give a warning on similiar C code) How I interpret that depends on my order of operations, which is pretty undefined for pseudo-code.
Thanatos
Thanks for the input, I just copied the code from the article and didn't notice the problem
Bozhidar Batsov
+3  A: 

For one thing, your algorithm is a bit off. It's more like:

if ( (year % 4 == 0) && ( year % 100 != 0 || year % 400 == 0 ))
    printf("%d is a leap year.\n", year);
 else
    printf("%d is not a leap year.\n", year);

Also, the Gregorian calendar was not adopted until 1582 or so.

Edit:

The Gregorian calendar was decreed in 1582, but adopted by various countries at various times, as late as 1926 in Turkey.

Adam Crume
It was adopted at different times in different places. Russia didn't adopt until the 20th century, for example.
John Y
It was adopted in September 1752 in the UK, hence the 'interesting' month for that year, since the calendar displayed up to then is Julian rather than Gregorian.
Pete Kirkham
That's a good point. I think 1582 might be the year it was proposed.
Adam Crume
A: 

"But is this specific to Gregorian calendar"

Yes.

Please read this http://en.wikipedia.org/wiki/Leap_year

Because of the orbits of Earth (and Moon), we have a variety of calendars, each with complex rules for trying to get a rational match between days and years.

Please read this http://en.wikipedia.org/wiki/Gregorian_calendar

Please buy a copy of http://emr.cs.uiuc.edu/home/reingold/calendar-book/index.shtml

S.Lott
A: 

Read down the web page you linked to. The reason for the gap in September 1752 is that is when the UK converted from Julian to Gregorian calendars, it is not a gap in Gregorian calendar but the difference between the two.

The page you link to uses the Julian calendar up until that point. Therefore the algorithm you use for calculating leap years is different to the way the page (or the UK, or England, though neither existed in AD 100) calculated for years before 1752.

Pete Kirkham
+2  A: 

I think it's important to separate the idea of historical dates on the one hand and date calculation on the other. Historical dates are subject to all kinds of political, social, and technological issues, and are thus not very suitable for a simple algorithm. See this page for the wide disparity in the adoption of the modern Gregorian calendar.

The usual practice when calculating dates is to forget about history and just pretend that the existing system has always been in place and will always be in place. (Unless you are an astronomer or certain other kinds of scientist, in which case you would use something even more accurate, and would be even less interested in historical dates.) If your application is all about recorded historical dates, then you are going to need a way to map those dates (which depend on the country, the period in history, etc.) to a more calculation-friendly universal dating system anyway.

John Y
A: 

If your platform has a Date datatype, your first try would be to use whatever date expression and manipulation features it has. Usually these are sufficient, and you can save yourself the trouble of taking responsibility for supporting YA date-handling library.

So the answer to your question would be the equivalent of:

if(exists("02/29/yyyy".toDate)) ...

le dorfier