This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed last year.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(27)
一年的长度(或多或少)为 365.242196 天。
因此,我们必须或多或少地减去四分之一天以使其适合:
365.242196 - 0.25 = 364.992196(通过在 4 年中添加 1 天):但是哎呀,现在太小了!! 让我们添加百分之一的一天(一百年不添加一次:-))
364.992196 + 0,01 = 365.002196(哎呀,有点太大了,无论如何,让我们在大约 400 年中添加一次这一天)
365.002196 - 1/400 = 364.999696
现在差不多了,只要时不时地玩一下闰秒,你就可以了放。
(注意:在此步骤之后不再应用更多更正的原因是因为一年的长度也发生变化!!,这就是为什么闰秒是最灵活的解决方案,请参见示例 这里)
这就是我猜的原因
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
wikipedia 上有一个算法来确定闰年:
<上有很多关于此主题的信息。 a href="http://en.wikipedia.org/wiki/Leap_year" rel="noreferrer">关于闰年的维基百科页面,包含有关不同日历的信息。
There's an algorithm on wikipedia to determine leap years:
There's a lot of information about this topic on the wikipedia page about leap years, inclusive information about different calendars.
一般来说,计算闰年的算法如下...
如果一年能被 4 整除但不能被 100 整除,则该年份为闰年。如果一年能被 4 和 100 整除,则该年份不是闰年年份,除非它也能被 400 整除。
因此,诸如 1996、1992、1988 等年份是闰年,因为它们可以被 4 整除,但不能被 100 整除。对于世纪年,400 规则很重要。 因此,世纪年份 1900、1800 和 1700 虽然仍能被 4 整除,但也能被 100 整除。由于它们不能再被 400 整除,因此它们不是闰年
In general terms the algorithm for calculating a leap year is as follows...
A year will be a leap year if it is divisible by 4 but not by 100. If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400.
Thus years such as 1996, 1992, 1988 and so on are leap years because they are divisible by 4 but not by 100. For century years, the 400 rule is important. Thus, century years 1900, 1800 and 1700 while all still divisible by 4 are also exactly divisible by 100. As they are not further divisible by 400, they are not leap years
这足以检查一年是否是闰年。
this is enough to check if a year is a leap year.
下面是使用 JavaScript 三元运算符的 wikipedia 算法 的简单实现:
Here is a simple implementation of the wikipedia algorithm, using the javascript ternary operator:
a) 一年有 365.242199 天。
b) 如果每年有 365 天,那么 100 年后我们将失去 24.2199 天。
这就是为什么我们每个世纪增加 24 天(每 4 年增加一次,除非能被 100 整除)
c) 但我们仍然损失 0.21299 天/世纪。 所以 4 个世纪后我们损失了 0.8796 天。
这就是为什么我们每 4 个世纪增加 1 天(每第 4 个世纪我们计算一个闰年)。
d) 但这意味着我们每四百年(4 个世纪)损失 -0.1204 天(我们前进)。 因此,在 8 个四百年(3200 年)中,我们不计算闰年。
e) 但这意味着每 3200 年我们会损失 0.0368 天。 因此,在 24x3200 年(=76800 年)中,我们损失了 0.8832 天。 这就是为什么我们要计算闰年。
等等...(到那时我们已经摧毁了地球,所以没关系)
但我无法理解的是,为什么我们不每 500 年而不是 400 年计算一次闰年。这样我们就能更快地收敛到正确的时间(我们会损失 2.3 小时/500 年)。
a) The year is 365.242199 days.
b) If every year was 365 days, in 100 years we would lose 24.2199 days.
That's why we add 24 days per century (every 4 years EXCEPT when divisible by 100)
c) But still we lose 0.21299 days/century. So in 4 centuries we lose 0.8796 days.
That's why we add 1 day per 4 centuries (every fourth century we DO count a leap year).
d) But that means we lose -0.1204 days (we go forward) per quadricentennial (4 centuries). So in 8 quadricentennial (3200 years) we DO NOT count a leap year.
e) But that means we lose 0.0368 days per 3200 years. So in 24x3200 years (=76800years) we lose 0.8832 days. That's why we DO count a leap year.
and so on... (by then we will have destroyed the planet, so it doesn't matter)
What I cannot understand though, is why we don't count a leap year every 500 years instead of 400. In that way we would converge more rapidly to the correct time (we would lose 2.3 hours/500 years).
我确信 维基百科 可以比我更好地解释它,但这基本上与事实上,如果每四年增加一天,我们就会领先于太阳,因为它绕太阳运行的时间少于 365.25 天,因此我们通过在不能被 400 整除的年份上不添加闰日来补偿这一点,例如1900.
希望有帮助
I'm sure Wikipedia can explain it better than I can, but it is basically to do with the fact that if you added an extra day every four years we'd get ahead of the sun as its time to orbit the sun is less than 365.25 days so we compensate for this by not adding leap days on years that are not divisible by 400 eg 1900.
Hope that helps
如果输入年份是闰年,则返回 true
基本现代代码:
今天的规则始于公元 1582 年
儒略历规则每四年开始于公元前 46 年,但在公元 10 年之前与凯撒宣布的不一致。
然而,他们确实每隔三年添加一些闰年,然后在之前的几年中:
因此,闰年为 45 BC、42 BC、39 BC、36 BC、33 BC、30 BC、27 BC、24 BC、21 BC、18 BC、15 BC、12 BC、9 BC、8 AD、12 AD
公元前 45 年之前没有添加闰年。
0 年不存在,因为它是...2BC 1BC 1AD 2AD...对于某些计算来说这可能是一个问题。
Return true if the input year is a leap year
Basic modern day code:
Todays rule started 1582 AD
Julian calendar rule with every 4th year started 46BC but is not coherent before 10 AD as declared by Cesar.
They did however add some leap years every 3rd year now and then in the years before:
Leap years were therefore 45 BC, 42 BC, 39 BC, 36 BC, 33 BC, 30 BC, 27 BC, 24 BC, 21 BC, 18 BC, 15 BC, 12 BC, 9 BC, 8 AD, 12 AD
Before year 45BC leap year was not added.
The year 0 do not exist as it is ...2BC 1BC 1AD 2AD... for some calculation this can be an issue.
你真的应该先尝试谷歌。
维基百科有闰年的解释。 您描述的算法适用于Proleptic 公历。
有关数学的更多信息,请参阅文章日历算法(PDF)。
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 (PDF).
如果我们再进一步,岂不是更好吗?
假设每3200年为无闰年,
一年的长度将会到来
,在此之后,大约 120000 年之后将需要进行调整。
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
and after this the adjustment will be required after around 120000 years.
在 Java 中,下面的代码计算两个给定年份之间的闰年计数。 确定循环的起点和终点。
然后,如果参数模 4 等于 0,并且参数模 100 不等于 0 或参数模 400 等于 0,则为闰年并增加计数器。
In
Java
Below code calculates leap year count between two given year. Determine starting and ending point of the loop.Then if parameter modulo 4 is equal 0 and parameter modulo 100 not equal 0 or parameter modulo 400 equal zero then it is leap year and increase counter.
PHP:
PHP:
如果年份是闰年,BIS 将为 1,否则在此布尔逻辑中为 0:
BIS will be 1 if the year is leap, otherwise 0 in this boolean logic:
闰年是任意的,用于描述闰年的系统是人为构建的。 没有为什么。
我的意思是,本来可以每 28 年有一个闰年,而我们在那些闰年里会多出一周……但权力决定每 4 年有一天来赶上。
它还与地球绕太阳公转一周需要 365.25 天等有关。当然,它并不是真正的 365.25,只是稍微少一点(365.242222...),所以要纠正由于这种差异,他们决定放弃可被 100 整除的闰年。
Leap years are arbitrary, and the system used to describe them is a man made construct. There is no why.
What I mean is there could have been a leap year every 28 years and we would have an extra week in those leap years ... but the powers that be decided to make it a day every 4 years to catch up.
It also has to do with the earth taking a pesky 365.25 days to go round the sun etc. Of course it isn't really 365.25 is it slightly less (365.242222...), so to correct for this discrepancy they decided drop the leap years that are divisible by 100.
如果您对这些规则的原因感兴趣,那是因为地球绕太阳公转一圈所需的时间是一个很长的不精确的十进制值。 它不完全是 365.25。 它略小于 365.25,因此每 100 年必须消除一个闰日 (365.25 - 0.01 = 365.24)。 但这也不完全正确。 该值略大于365.24。 因此,100 年规则只有四分之三适用(或者换句话说,每 400 年加回 1 天;365.25 - 0.01 + 0.0025 = 365.2425)。
If you're interested in the reasons for these rules, it's because the time it takes the earth to make exactly one orbit around the sun is a long imprecise decimal value. It's not exactly 365.25. It's slightly less than 365.25, so every 100 years, one leap day must be eliminated (365.25 - 0.01 = 365.24). But that's not exactly correct either. The value is slightly larger than 365.24. So only 3 out of 4 times will the 100 year rule apply (or in other words, add back in 1 day every 400 years; 365.25 - 0.01 + 0.0025 = 365.2425).
目前,一年平均大约有 365.2425 天(地球正在减速,但我们暂时忽略这一点)。
我们每 4 年就有一次闰年的原因是,这使我们平均达到 365.25
[(365+365+365+366) / 4 = 365.25,4 年 1461 天]
。我们在 100 倍数上没有闰年的原因是为了让我们得到 365.24 `[(1461 x 25 - 1) / 100 = 365.24,即 100 年有 36,524 天。
那么我们再次在 400 倍数上设置闰年的原因是为了让我们得到 365.2425
[(36,524 x 4 + 1) / 400 = 365.2425, 400 年有 146,097 天]
。我相信在 3600 倍数上可能还有另一条规则,但我从未为其编写过代码(Y2K 是一回事,但在我看来,没有必要为未来 5000 年进行规划 - 请记住,我一直在之前写错了)。
因此,规则的优先级依次递减:
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:
这里有一个相当晦涩的想法。
当每年能被100整除的时候有365天,这个时候该怎么办呢? 在遥远的未来,即使是能被400整除的年份也只能得到365天。
那么就有可能或者有理由对能被80整除的年份进行修正。
正常年份有365天,能被400整除的年份有366天。 或者这是一种松散的情况。
Here comes a rather obsqure idea.
When every year dividable with 100 gets 365 days, what shall be done at this time? In the far future, when even years dividable with 400 only can get 365 days.
Then there is a possibility or reason to make corrections in years dividable with 80.
Normal years will have 365 day and those dividable with 400 can get 366 days. Or is this a loose-loose situation.
您可以只检查年份数字是否能被 4 和 400 整除。您实际上不需要检查它是否不能被 100 整除。400 出现问题的原因是因为根据公历,我们的“昼长”稍微长一些。为了弥补这一点,我们有 303 个平年(每年 365 天)和 97 个闰年(每年 366 天)。 这 3 个额外年份(不是闰年)的区别在于与公历保持循环,每 400 年重复一次。 查找克里斯蒂安·泽勒的同余方程。 这将有助于理解真正的原因。 希望这可以帮助 :)
You could just check if the Year number is divisible by both 4 and 400. You dont really need to check if it is indivisible by 100. The reason 400 comes into question is because according to the Gregorian Calendar, our "day length" is slightly off, and thus to compensate that, we have 303 regular years (365 days each) and 97 leap years (366 days each). The difference of those 3 extra years that are not leap years is to stay in cycle with the Gregorian calendar, which repeats every 400 years. Look up Christian Zeller's congruence equation. It will help understanding the real reason. Hope this helps :)
在公历中,必须考虑 3 个标准来确定闰年:
In the Gregorian calendar 3 criteria must be taken into account to identify leap years:
Python 3.5
Python 3.5
简单地
因为2000年是闰年,可以被100整除,也可以被4整除。
所以为了保证它是正确的跳跃,我们需要确保它能被 400 整除。
2000%4=0
2000%100=0
根据算法,它不是跳跃,但可以被400整除
2000%400=0
所以它是飞跃。
Simply
Because year 2000 is a leap year and it is divisible by 100 and dividable by 4.
SO to guarantee it is correct leap we need to ensure it is divisible by 400.
2000 % 4 = 0
2000 % 100 = 0
According to algorithm it's not leap, but it is dividable by 400
2000 % 400 = 0
so it is leap.
我在《Python 3图解指南》一书中发现了这个问题。 在很早的一章中,只讨论了数学运算,没有循环,没有比较,没有条件。 如何判断某一年是否为闰年?
以下是我的想法:
I found this problem in the book "Illustrated Guide to Python 3". It was in a very early chapter that only discussed the math operations, no loops, no comparisons, no conditionals. How can you tell if a given year is a leap year?
Below is what I came up with:
我认为这是最有效的方法。
Python:
This is the most efficient way, I think.
Python:
C# 实现
C# implementation
从1700年到1917年,官方历法是儒略历。 从那时起我们就使用公历系统。 从儒略历系统到公历系统的过渡发生在 1918 年,当时 1 月 31 日之后的第二天是 2 月 14 日。 这意味着1918年的第32天,是2月14日。
在这两个日历系统中,二月是唯一天数可变的月份,闰年有 29 天,其他年份有 28 天。 在儒略历中,闰年可以被 4 整除,而在公历中,闰年可以是以下任意一种:
可被 400 整除。
可被 4 整除且不能被 100 整除。
因此,闰年的程序将是:
Python:
From 1700 to 1917, official calendar was the Julian calendar. Since then they we use the Gregorian calendar system. The transition from the Julian to Gregorian calendar system occurred in 1918, when the next day after January 31st was February 14th. This means that 32nd day in 1918, was the February 14th.
In both calendar systems, February is the only month with a variable amount of days, it has 29 days during a leap year, and 28 days during all other years. In the Julian calendar, leap years are divisible by 4 while in the Gregorian calendar, leap years are either of the following:
Divisible by 400.
Divisible by 4 and not divisible by 100.
So the program for leap year will be:
Python:
在shell中你可以使用
cal -j YYYY
打印一年中的儒略日,如果最后一个儒略日是366,那么它是闰年。使用 awk,你可以这样做
In shell you can use
cal -j YYYY
which prints the julian day of the year, If the last julian day is 366, then it is a leap year.Using awk, you can do
刚刚在 Coffee-Script 中写了这个:
当然,这里完成的有效性检查比要求的要进一步一些,但我发现这是良好编程中必要的事情。
请注意,此函数的返回值表示所谓的预生公历中的闰年,因此对于 1400 年,它表示
false
,而实际上该年是闰年,根据当时使用的儒略历。 我仍然会将其保留在我正在编写的日期时间库中,因为编写正确的代码来处理日期很快会令人惊讶地参与其中,所以我只会支持公历(或为另一个日历付费)。just wrote this in Coffee-Script:
of course, the validity checking done here goes a little further than what was asked for, but i find it a necessary thing to do in good programming.
note that the return values of this function indicate leap years in the so-called proleptic gregorian calendar, so for the year 1400 it indicates
false
, whereas in fact that year was a leap year, according to the then-used julian calendar. i will still leave it as such in the datetime library i'm writing because writing correct code to deal with dates quickly gets surprisingly involved, so i will only ever support the gregorian calendar (or get paid for another one).