-
Notifications
You must be signed in to change notification settings - Fork 0
/
timehelper.go
34 lines (30 loc) · 940 Bytes
/
timehelper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package timehelper
import (
"time"
)
// TimeYMD returns [time.Time] corresponding to 'year', 'month' and 'day'.
// The returned value has zero clock and [time.UTC] time zone.
func TimeYMD(year int, month time.Month, day int) time.Time {
return time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
}
// IsLeapYear reports whether the 'year' is leap.
func IsLeapYear(year int) bool {
return year%4 == 0 && year%100 != 0 || year%400 == 0
}
// DaysInMonth returns number of days in the 'month' of the 'year'.
// If 'month' is invalid -1 is returned. Use [ValidMonth] if need month validation.
func DaysInMonth(year int, month time.Month) int {
switch month {
case time.February:
if IsLeapYear(year) {
return 29
}
return 28
case time.April, time.June, time.September, time.November:
return 30
case time.January, time.March, time.May, time.July, time.August, time.October, time.December:
return 31
default:
return -1
}
}