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
This commit is contained in:
Peter Mundy 2011-12-07 14:47:25 -05:00 committed by Russ Cox
parent 127b5a66b1
commit 69191553e7
3 changed files with 25 additions and 1 deletions

View File

@ -10,3 +10,4 @@ func init() {
}
var Interrupt = interrupt
var DaysIn = daysIn

View File

@ -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.

View File

@ -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()