mirror of
https://github.com/golang/go
synced 2024-11-22 11:24:49 -07:00
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:
parent
127b5a66b1
commit
69191553e7
@ -10,3 +10,4 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var Interrupt = interrupt
|
var Interrupt = interrupt
|
||||||
|
var DaysIn = daysIn
|
||||||
|
@ -673,7 +673,7 @@ func daysIn(m Month, year int) int {
|
|||||||
if m == February && isLeap(year) {
|
if m == February && isLeap(year) {
|
||||||
return 29
|
return 29
|
||||||
}
|
}
|
||||||
return int(daysBefore[m+1] - daysBefore[m])
|
return int(daysBefore[m] - daysBefore[m-1])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provided by package runtime.
|
// 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) {
|
func BenchmarkNow(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
Now()
|
Now()
|
||||||
|
Loading…
Reference in New Issue
Block a user