-
Notifications
You must be signed in to change notification settings - Fork 8
/
check.go
38 lines (27 loc) · 899 Bytes
/
check.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
35
36
37
38
package week
import (
"time"
"github.com/pkg/errors"
)
// checkYearAndWeek tests if a week and year are valid values for an ISO 8601 week date. If one of the
// provided values is invalid the function returns a detailed error.
func checkYearAndWeek(year, week int) error {
if year < 0 || year > 9999 {
return errors.Errorf("year must be between 0 and 9999 but was %d", year)
}
maxWeeks := weeksInYear(year)
if week < 1 || week > maxWeeks {
return errors.Errorf("week in %d must be between 1 and %d but was %d", year, maxWeeks, week)
}
return nil
}
func weeksInYear(year int) int {
firstWeekday := time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC).Weekday()
if firstWeekday == time.Thursday || (isLeapYear(year) && firstWeekday == time.Wednesday) {
return 53
}
return 52
}
func isLeapYear(year int) bool {
return (year%400 == 0 || year%100 != 0) && (year%4 == 0)
}