-
Notifications
You must be signed in to change notification settings - Fork 5
/
goholiday.go
69 lines (55 loc) · 1.6 KB
/
goholiday.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
package goholiday
import "time"
const dateFormat = "2006-01-02"
type Schedule interface {
GetNationalHolidays() map[string]string
GetWeekdayHolidays() map[time.Weekday]struct{}
}
type Goholiday struct {
schedule Schedule
uniqueHolidays map[string]struct{}
}
func New(schedule Schedule) *Goholiday {
return &Goholiday{
schedule: schedule,
uniqueHolidays: map[string]struct{}{},
}
}
func (g *Goholiday) IsNationalHoliday(t time.Time) bool {
_, exist := g.schedule.GetNationalHolidays()[t.Format(dateFormat)]
return exist
}
func (g *Goholiday) IsHoliday(t time.Time) bool {
return g.isWeekdayHoliday(t) || g.IsNationalHoliday(t) || g.isUniqueHoliday(t)
}
func (g *Goholiday) isWeekdayHoliday(t time.Time) bool {
_, exist := g.schedule.GetWeekdayHolidays()[t.Weekday()]
return exist
}
func (g *Goholiday) SetUniqueHolidays(ts []time.Time) {
for _, t := range ts {
g.uniqueHolidays[t.Format(dateFormat)] = struct{}{}
}
}
func (g *Goholiday) isUniqueHoliday(t time.Time) bool {
_, exist := g.uniqueHolidays[t.Format(dateFormat)]
return exist
}
func (g *Goholiday) IsBusinessDay(t time.Time) bool {
return !g.IsHoliday(t)
}
func (g *Goholiday) BusinessDaysBefore(t time.Time, bds int) time.Time {
return g.travelBusinessDays(t, bds, -1)
}
func (g *Goholiday) BusinessDaysAfter(t time.Time, bds int) time.Time {
return g.travelBusinessDays(t, bds, 1)
}
func (g *Goholiday) travelBusinessDays(t time.Time, bds int, course int) time.Time {
duration := time.Hour * 24 * time.Duration(course)
for tbds := 0; tbds != bds; {
if t = t.Add(duration); !g.IsHoliday(t) {
tbds++
}
}
return t
}