Vincent,
The package time API is a contract. From the API:
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
Therefore, we know that there are no leap seconds.
Internally, the package time assumes that there are 24*time.Hour nanoseconds per day.
const (
secondsPerMinute = 60
secondsPerHour = 60 * 60
secondsPerDay = 24 * secondsPerHour
secondsPerWeek = 7 * secondsPerDay
daysPer400Years = 365*400 + 97
daysPer100Years = 365*100 + 24
daysPer4Years = 365*4 + 1
days1970To2001 = 31*365 + 8
)
But, to be really sure, add a test for the diffDays function.
func diffDays(
year2 int, month2 time.Month, day2 int,
year1 int, month1 time.Month, day1 int,
) int {
d2 := time.Date(
year2, month2, day2,
0, 0, 0, 0, time.UTC,
)
d1 := time.Date(
year1, month1, day1,
0, 0, 0, 0, time.UTC,
)
return int(d2.Sub(d1) / (24 * time.Hour))
}
For example,
import (
"testing"
"time"
)
type testDiffDay struct {
y2, m2, d2 int
y1, m1, d1 int
days int
}
var testDiffDays = []testDiffDay{
// 1 - 1 = 0
{2000, 1, 1, 2000, 1, 1, 0},
// 2 - 1 = 1
{2000, 1, 1 + 1, 2000, 1, 1, 1},
// 1 - 2 = -1
{2000, 1, 1, 2000, 1, 1 + 1, -1},
// (366 - 1) + (365*290 + 290/4 - 290/100) = 106285
{2000 + 290, 12, 31, 2000, 1, 1, 106285},
// (366 - 1) + (365*290 + 290/4 - 290/100) = 106285
{2000, 12, 31, 2000 - 290, 1, 1, 106285},
}
func Test24HoursPerDay(t *testing.T) {
for _, test := range testDiffDays {
days := diffDays(
test.y2, time.Month(test.m2), test.d2,
test.y1, time.Month(test.m1), test.d1,
)
if days != test.days {
t.Errorf("diffDays: found %d expected %d", days, test.days)
}
}
}
Peter