From 69191553e7a67f63c21d79caf1881ee7487cb6bc Mon Sep 17 00:00:00 2001 From: Peter Mundy Date: Wed, 7 Dec 2011 14:47:25 -0500 Subject: [PATCH] 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 --- src/pkg/time/internal_test.go | 1 + src/pkg/time/time.go | 2 +- src/pkg/time/time_test.go | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/pkg/time/internal_test.go b/src/pkg/time/internal_test.go index 2c4df335f9..b753896d77 100644 --- a/src/pkg/time/internal_test.go +++ b/src/pkg/time/internal_test.go @@ -10,3 +10,4 @@ func init() { } var Interrupt = interrupt +var DaysIn = daysIn diff --git a/src/pkg/time/time.go b/src/pkg/time/time.go index 04ed86cf25..4e9accfe58 100644 --- a/src/pkg/time/time.go +++ b/src/pkg/time/time.go @@ -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. diff --git a/src/pkg/time/time_test.go b/src/pkg/time/time_test.go index 6d1e79b542..464e9bfa2c 100644 --- a/src/pkg/time/time_test.go +++ b/src/pkg/time/time_test.go @@ -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() -- 2.48.1