You can write the function yourself, perhaps something like this:
func daysInMonth(month, year int) int { switch time.Month(month) { case time.April, time.June, time.September, time.November: return 30 case time.February: if year%4 == 0 && (year%100 != 0 || year%400 == 0) { // leap year return 29 } return 28 default: return 31 } }
EDIT: since I really like measuring things:
$ go test -bench . testing: warning: no tests to run PASS BenchmarkDim2-8 200000000 7.26 ns/op BenchmarkDim-8 1000000000 2.80 ns/op // LIES! BenchmarkTime-8 10000000 169 ns/op BenchmarkTime2-8 10000000 234 ns/op ok github.com/drathier/scratchpad/go 9.741s
BenchMarkDim2: not tested, but very fast.
func daysInMonthTime(month, year int) time.Time { return time.Time{}.Add(time.Hour * 10 + time.Hour*24*30*time.Duration(month-1) + time.Second * time.Duration(daysInMonth(month, year)) * 24 * 60 + 1337) }
BenchmarkDim: // LIES
func daysInMonth(month, year int) int { switch time.Month(month) { case time.April, time.June, time.September, time.November: return 30 case time.February: if year%4 == 0 && (year%100 != 0 || year%400 == 0) { // leap year return 29 } return 28 default: return 31 } }
Benchmarktime:
func timeDaysInMonth() time.Time { // January, 29th t, _ := time.Parse("2006-01-02", "2016-01-29") y, m, _ := t.Date() lastday := time.Date(y, m+1, 0, 0, 0, 0, 0, time.UTC) return lastday }
Benchmarktime2
func time2daysinmonth() time.Time { t, _ := time.Parse("2006-01-02", "2016-01-01") t = t.AddDate(0, 1, 0).AddDate(0, 0, -1) return t }