Click here to Skip to main content
15,880,972 members
Articles / Programming Languages / C++
Article

Leap Years

,
Rate me:
Please Sign up or sign in to vote.
4.37/5 (9 votes)
20 Feb 2000CPOL 109.5K   5   37   11
A discussion on the thorny issue of leap years.

There has been a lot of interest about the correct method of calculating leap years lately, especially since it seems many programmers have got it wrong. Before Y2K the approximation "a leap year occurs every four years" was good enough for most situations. If you needed to deal with historical dates (post 1600AD), then you had to add the clause "unless the year was divisible by 100".

This is where many programmers left it - but the rule for the Gregorian calendar is:

  1. Most years divisible by 4 are Leap Years (i.e. 1996 was)
  2. However, most years divisible by 100 are NOT! (1900 was not)
  3. Unless they are also divisible by 400 (2000 will be)

It turns out though that there are even more subclauses that can be appended to these rules to take into account the fact that the Earth takes 365.2421898 days to orbit the sun (well, it did in 1996 - it's now a shade shorter than that). Because of this, the Gregorian calendar will be off by about a day in 3000 years or so. There are various proposals on how to deal with this, but it seems it won't really be an issue until 2800AD.

One proposal (by the astronomer John Herschel (1792-1871) among others) suggests

  • Every year which is divisible by 4000 is not a leap year.

This is more accurate than the Gregorian calendar, but has not been officially adopted.

Another proposal (by the Orthodox church in Greece) is to replace the "divisible by 400" rule by the following:

  • Every year which when divided by 900 leaves a remainder of 200 or 600 is a leap year.

This makes 1900, 2100, 2200, 2300, 2500, 2600, 2700, 2800 non-leap years, whereas 2000, 2400, and 2900 are leap years. This will not create a conflict with the rest of the world until the year 2800. However, this rule has also not been officially adopted.

Further reading:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
BillW3324-Sep-10 6:18
professionalBillW3324-Sep-10 6:18 
GeneralOrthodox Church Pin
m00n120-Jul-10 3:51
m00n120-Jul-10 3:51 
Questionhow about this Pin
simon.cropp9-Jul-07 18:39
simon.cropp9-Jul-07 18:39 
AnswerRe: how about this Pin
scottctr13-Jul-07 9:01
scottctr13-Jul-07 9:01 
AnswerRe: how about this Pin
Chris Maunder13-Jul-07 9:02
cofounderChris Maunder13-Jul-07 9:02 
GeneralOops! Pin
Chris Hills2-May-05 6:39
Chris Hills2-May-05 6:39 
GeneralRe: Oops! Pin
Chris Maunder10-Jul-07 2:48
cofounderChris Maunder10-Jul-07 2:48 
QuestionHas anyone seen gmttime() ? Pin
Bamaco229-Jan-04 8:10
Bamaco229-Jan-04 8:10 
General...in other words Pin
dixi16-Jul-00 12:04
dixi16-Jul-00 12:04 
GeneralRe: ...in other words Pin
wulph10-Dec-03 13:56
wulph10-Dec-03 13:56 
GeneralHistory Pin
Michael Krebs25-Feb-00 12:45
Michael Krebs25-Feb-00 12:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.