Commit 69191553 authored by Peter Mundy's avatar Peter Mundy Committed by Russ Cox

time: fix daysIn for December

daysBefore[12+1]: index out of range
time.December and Windows SYSTEMTIME.wMonth
are 12 for December.

R=rsc, dsymonds
CC=golang-dev
https://golang.org/cl/5448130
parent 127b5a66
......@@ -10,3 +10,4 @@ func init() {
}
var Interrupt = interrupt
var DaysIn = daysIn
......@@ -673,7 +673,7 @@ func daysIn(m Month, year int) int {
if m == February && isLeap(year) {
return 29
}
return int(daysBefore[m+1] - daysBefore[m])
return int(daysBefore[m] - daysBefore[m-1])
}
// Provided by package runtime.
......
......@@ -632,6 +632,29 @@ func TestDate(t *testing.T) {
}
}
var daysInTests = []struct {
year, month, di int
}{
{2011, 1, 31}, // January, first month, 31 days
{2011, 2, 28}, // February, non-leap year, 28 days
{2012, 2, 29}, // February, leap year, 29 days
{2011, 6, 30}, // June, 30 days
{2011, 12, 31}, // December, last month, 31 days
}
func TestDaysIn(t *testing.T) {
// The daysIn function is not exported.
// Test the daysIn function via the `var DaysIn = daysIn`
// statement in the internal_test.go file.
for _, tt := range daysInTests {
di := DaysIn(Month(tt.month), tt.year)
if di != tt.di {
t.Errorf("got %d; expected %d for %d-%02d",
di, tt.di, tt.year, tt.month)
}
}
}
func BenchmarkNow(b *testing.B) {
for i := 0; i < b.N; i++ {
Now()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment