在Go语言的`time.go`文件中,为什么`daysPer100Years`的值是365*100 + 24?

huangapple go评论80阅读模式
英文:

in golang go/src/time.go, why daysPer100Years = 365*100 + 24?

问题

我正在阅读package time的Golang源代码,并且遇到了一些常量,如下所示:

// 文件路径:/usr/local/go/src/time/time.go
const (
	secondsPerMinute = 60
	secondsPerHour   = 60 * secondsPerMinute
	secondsPerDay    = 24 * secondsPerHour
	secondsPerWeek   = 7 * secondsPerDay
	daysPer400Years  = 365*400 + 97
	daysPer100Years  = 365*100 + 24
	daysPer4Years    = 365*4 + 1
)

我想知道为什么是这样的:

daysPer400Years  = 365*400 + 97
daysPer100Years  = 365*100 + 24

而不是这样的:

daysPer400Years  = 365*400 + 100
daysPer100Years  = 365*100 + 25
英文:

I'm reading golang source code of package time
And I got some constants like:

// file: /usr/local/go/src/time/time.go
const (
	secondsPerMinute = 60
	secondsPerHour   = 60 * secondsPerMinute
	secondsPerDay    = 24 * secondsPerHour
	secondsPerWeek   = 7 * secondsPerDay
	daysPer400Years  = 365*400 + 97
	daysPer100Years  = 365*100 + 24
	daysPer4Years    = 365*4 + 1
)

I'm wondering why

daysPer400Years  = 365*400 + 97
daysPer100Years  = 365*100 + 24

but not

daysPer400Years  = 365*400 + 100
daysPer100Years  = 365*100 + 25

答案1

得分: 1

当我输入这个问题时,我自己得到了答案 😊 ----

通常,我们称年份X为闰年,如果X可以被4整除。然而,如果年份X可以被100整除但不能被400整除,我们称X不是闰年。

这意味着,每100年至少有24个闰年。在这100年中,有一年必须可以被100整除,但可能不能被400整除,这种情况发生的概率是3/4。换句话说,每100年,24个闰年的概率为75%,25个闰年的概率为25%。这就是为什么

daysPer100Years = 365*100 + 24

每400年,必定有3个年份可以被100整除但不能被400整除。
这就是为什么

daysPer400Years = 365*400 + 97
//即:daysPer400Years = 365*400 + (25 * 4 - 3)
//或者:daysPer400Years = 365*400 + (24 * 4 + 1)
英文:

When I'm typing this question, I got the answer myself 😂 ----

Usually we call a year X is a leap year if X is divisible by 4. While the year X is divisible by 100 but not divisible by 400, we say X is not a leap year.

That means, every 100 years, there are at least 24 leap years. Among the 100 years, there is a year which must be divisible by 100, but may not be divisible by 400, this phenomenon occurs by the probability of 3/4. In other words, every 100 years, the probability of 24 leap years is 75%, and 25% for 25 leap years. That's why

daysPer100Years  = 365*100 + 24

Every 400 years, there must be 3 years which can be divisible by 100 but not divisible by 400.
That's why

daysPer400Years  = 365*400 + 97
//that is : daysPer400Years  = 365*400 + (25 * 4 - 3)
//     or : daysPer400Years  = 365*400 + (24 * 4 + 1)

huangapple
  • 本文由 发表于 2021年5月25日 11:54:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/67681571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定