leap year calculation

2019-01-04 22:13发布

In order to find leap years, why must the year be indivisible by 100 and divisible by 400? I understand why it must be divisible by 4. Please explain the algorithm.

22条回答
时光不老,我们不散
2楼-- · 2019-01-04 22:55

The length of a year is (more or less) 365.242196 days. So we have to subtract, more or less, a quarter of a day to make it fit :

365.242196 - 0.25 = 364.992196 (by adding 1 day in 4 years) : but oops, now it's too small!! lets add a hundreth of a day (by not adding that day once in a hundred year :-))

364.992196 + 0,01 = 365.002196 (oops, a bit too big, let's add that day anyway one time in about 400 years)

365.002196 - 1/400 = 364.999696

Almost there now, just play with leapseconds now and then, and you're set.

(Note : the reason no more corrections are applied after this step is because a year also CHANGES IN LENGTH!!, that's why leapseconds are the most flexible solution, see for examlple here)

That's why i guess

查看更多
何必那么认真
3楼-- · 2019-01-04 22:55

Will it not be much better if we make one step further. Assuming every 3200 year as no leap year, the length of the year will come

364.999696 + 1/3200 = 364.999696 + .0003125 = 365.0000085

and after this the adjustment will be required after around 120000 years.

查看更多
We Are One
4楼-- · 2019-01-04 22:56

There's an algorithm on wikipedia to determine leap years:

function isLeapYear (year):
    if ((year modulo 4 is 0) and (year modulo 100 is not 0))
    or (year modulo 400 is 0)
        then true
    else false

There's a lot of information about this topic on the wikipedia page about leap years, inclusive information about different calendars.

查看更多
成全新的幸福
5楼-- · 2019-01-04 22:58

There are on average, roughly 365.2425 days in a year at the moment (the Earth is slowing down but let's ignore that for now).

The reason we have leap years every 4 years is because that gets us to 365.25 on average [(365+365+365+366) / 4 = 365.25, 1461 days in 4 years].

The reason we don't have leap years on the 100-multiples is to get us to 365.24 `[(1461 x 25 - 1) / 100 = 365.24, 36,524 days in 100 years.

Then the reason we once again have a leap year on 400-multiples is to get us to 365.2425 [(36,524 x 4 + 1) / 400 = 365.2425, 146,097 days in 400 years].

I believe there may be another rule at 3600-multiples but I've never coded for it (Y2K was one thing but planning for one and a half thousand years into the future is not necessary in my opinion - keep in mind I've been wrong before).

So, the rules are, in decreasing priority:

  • multiple of 400 is a leap year.
  • multiple of 100 is not a leap year.
  • multiple of 4 is a leap year.
  • anything else is not a leap year.
查看更多
聊天终结者
6楼-- · 2019-01-04 22:58

In the Gregorian calendar 3 criteria must be taken into account to identify leap years:

  1. The year is evenly divisible by 4;
  2. If the year can be evenly divided by 100, it is NOT a leap year, unless;
  3. The year is also evenly divisible by 400. Then it is a leap year. Why the year divided by 100 is not leap year
查看更多
ら.Afraid
7楼-- · 2019-01-04 22:59

You really should try to google first.

Wikipedia has a explanation of leap years. The algorithm your describing is for the Proleptic Gregorian calendar.

More about the math around it can be found in the article Calendar Algorithms.

查看更多
登录 后发表回答