KDOC 435: daysPerYearの値は何か?

この文書のステータス

  • 作成
    • 2025-09-08 貴島
  • レビュー
    • 2025-09-10 貴島

概要

Go言語の json packageには不思議な値がある。これはなにか。

// 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型に変換する。

func parseDurationISO8601(b []byte) (time.Duration, error) {

期間は、うるう年などがあるので開始期間がわからないと正確でない。が、継続時間形式では開始時間指定がなくてもよいので、開始時間なしで丸めるためにこの不思議な値が使われている。

関連