KDOC 435: daysPerYearの値は何か?
この文書のステータス
- 作成
- 2025-09-08 貴島
- レビュー
- 2025-09-10 貴島
概要
Go言語の json
packageには不思議な値がある。これはなにか。
https://github.com/golang/go/blob/11f11f2a00aa3149a6ea69a50e7b7b429cf368b7/src/encoding/json/v2/arshal_time.go#L469-L473
// daysPerYear is the exact average number of days in a year according to // the Gregorian calender, which has an extra day each year that is // a multiple of 4, unless it is evenly divisible by 100 but not by 400. // This does not take into account leap seconds, which are not deterministic. const daysPerYear = 365.2425
daysPerYear
はISO8601の継続時間フォーマットのパースに使われている。
P3Y6M4DT12H30M5S
のような文字列をGoのDuration型に変換する。
https://github.com/golang/go/blob/988a20c8c5e2c9eb49f8749e5ee94ce3c964fe59/src/encoding/json/v2/arshal_time.go#L520
func parseDurationISO8601(b []byte) (time.Duration, error) {
期間は、うるう年などがあるので開始期間がわからないと正確でない。が、継続時間形式では開始時間指定がなくてもよいので、開始時間なしで丸めるためにこの不思議な値が使われている。
関連
- KDOC 422: 継続時間形式を見る。継続時間に関する話題