golang源码(0) ---- go/src/time.go 中的常量

今天阅读 go/src/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

搜索引擎上搜索无果,遂赴 StackOverflow 发帖提问。正写着帖子,突然自己想明白了这个问题,遂自答之。
帖子如下:
https://stackoverflow.com/questions/67681571/in-golang-go-src-time-go-why-daysper100years-365100-24/67681572

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)
posted @ 2021-05-25 11:59  HorseShoe2016  阅读(137)  评论(0编辑  收藏  举报