-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar.go
72 lines (61 loc) · 1.26 KB
/
calendar.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
package calendar
import "time"
// IsWorkDay determines a date is a working day or not
func IsWorkDay(date time.Time) bool {
y, m, d := date.Date()
if is, ok := reschedule[y][m][d]; ok {
return is
} else if _, ok := defaultHolidays[m][d]; ok {
return false
} else if wd := date.Weekday(); wd == time.Saturday || wd == time.Sunday {
return false
}
return true
}
// Add return the date corresponding to adding the given number of working days to date
func Add(date time.Time, days int) time.Time {
var inc int
if days > 0 {
inc = 1
} else {
inc = -1
}
for {
if days == 0 {
break
}
date = date.AddDate(0, 0, inc)
if IsWorkDay(date) {
days -= inc
}
}
return date
}
// Period calculates count of working day between from and to
func Period(from, to time.Time) int {
var inc int
if from.Before(to) {
inc = 1
} else {
inc = -1
}
var days int
for {
if from.Equal(to) {
break
}
from = from.AddDate(0, 0, inc)
if IsWorkDay(from) {
days += inc
}
}
return days
}
var defaultHolidays = map[time.Month]map[int]struct{}{
time.January: {1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {}, 8: {}},
time.February: {23: {}},
time.March: {8: {}},
time.May: {1: {}, 9: {}},
time.June: {12: {}},
time.November: {4: {}},
}