-
Notifications
You must be signed in to change notification settings - Fork 0
/
holiday.go
99 lines (78 loc) · 1.7 KB
/
holiday.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package holiday
import (
"time"
)
var (
m = NewCalendarManager()
)
func NewCalendarManager() *CalendarManager {
return &CalendarManager{
f: func(year int) map[Date][]*Holiday {
return map[Date][]*Holiday{}
},
holidays: map[Date][]*Holiday{},
}
}
type CalendarManager struct {
f SetHolidaysFunc
holidays map[Date][]*Holiday
}
type Holiday struct {
Name string
Details string
}
type Date struct {
Month time.Month
Day int
}
func (c *CalendarManager) ClearHolidays() {
c.holidays = map[Date][]*Holiday{}
c.f = func(year int) map[Date][]*Holiday {
return map[Date][]*Holiday{}
}
}
func (c *CalendarManager) GetHolidays() map[Date][]*Holiday {
return c.holidays
}
func (c *CalendarManager) Add(date *Date, holiday *Holiday) {
if _, ok := c.holidays[*date]; !ok {
c.holidays = map[Date][]*Holiday{
*date: {
holiday,
},
}
}
c.holidays[*date] = append(c.holidays[*date], holiday)
}
func (c *CalendarManager) AddFromTime(t time.Time, holiday *Holiday) {
c.Add(&Date{Day: t.Day(), Month: t.Month()}, holiday)
}
func (c *CalendarManager) Holidays(t time.Time) []*Holiday {
holidays := c.f(t.Year())
return holidays[Date{
Day: t.Day(),
Month: t.Month(),
}]
}
func (c *CalendarManager) IsHoliday(t time.Time) bool {
return len(c.Holidays(t)) > 0
}
type SetHolidaysFunc func(year int) map[Date][]*Holiday
func Add(date *Date, holiday *Holiday) {
m.Add(date, holiday)
}
func AddFromTime(t time.Time, holiday *Holiday) {
m.AddFromTime(t, holiday)
}
func SetHolidays(f SetHolidaysFunc) {
m.f = f
}
func Holidays(t time.Time) []*Holiday {
return m.Holidays(t)
}
func IsHoliday(t time.Time) bool {
return m.IsHoliday(t)
}
func ClearHolidays() {
m.ClearHolidays()
}