-
Notifications
You must be signed in to change notification settings - Fork 1
/
ausholiday.go
179 lines (148 loc) · 3.63 KB
/
ausholiday.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2019 Ninja Software. All rights reserved.
// Use of this source code is governed by a
// license that can be found in the LICENSE file.
// Package ausholiday give easy access to check for Australian holiday
//
// Can be used immediately or create as a instance with custom holiday dates. Both singleton and instance ausholiday have same functions.
package ausholiday
import (
"strings"
"sync"
"time"
)
// Version of release for ausholiday
const Version = "v1.1.2"
var onceAusHoliday sync.Once
var xholidays []*Holiday
// AusHoliday struct for australia holiday
type AusHoliday struct {
Holidays []*Holiday
}
// Holiday date
type Holiday struct {
Date time.Time
YMD string // yyyymmdd
HolidayName string
Information string
MoreInformation string
Jurisdiction State
}
// State of Australia
type State string
// State data
// https://en.wikipedia.org/wiki/States_and_territories_of_Australia
// order: abbrev, iso
const (
WA State = "wa"
NSW State = "nsw"
QLD State = "qld"
SA State = "sa"
TAS State = "tas"
VIC State = "vic"
NT State = "nt"
ACT State = "act"
JBT State = "jbt"
AAT State = "aat"
CT State = "ct" // ISO Christmas Island
CC State = "cc" // ISO Cocos (Keeling) Islands
CSI State = "csi" // Coral Sea Islands
HIMI State = "himi" // Heard Island and McDonald Islands
NF State = "nf" // ISO NF
)
// States string map of states for quick matching
var States = map[string]State{
"wa": WA,
"nsw": NSW,
"qld": QLD,
"sa": SA,
"tas": TAS,
"vic": VIC,
"nt": NT,
"act": ACT,
"jbt": JBT,
"aat": AAT,
"ct": CT,
"cc": CC,
"csi": CSI,
"himi": HIMI,
"nf": NF,
}
// inputCSV csv file structure
type inputCSV struct {
Date string `csv:"Date"`
HolidayName string `csv:"Holiday Name"`
Information string `csv:"Information"`
MoreInformation string `csv:"More Information"`
Jurisdiction string `csv:"Jurisdiction"`
}
func init() {
// load as singleton, loads built-in holidays
onceAusHoliday.Do(func() {
localCSV := strings.NewReader(csvStr)
days, err := NewFromReader(localCSV)
if err != nil {
panic(err)
}
xholidays = days.Holidays
})
}
// Holiday alternative
type HolidayAlt struct {
HolidayName string
Information string
MoreInformation string
Jurisdiction State
Timezone string
Month time.Month
Week int
Weekday time.Weekday
}
var holidayAlts = []*HolidayAlt{
{
HolidayName: "Dawin Show Day",
Information: "",
MoreInformation: "",
Jurisdiction: NT,
Timezone: "Australia/Darwin",
Month: time.July,
Week: 4,
Weekday: time.Friday,
},
}
func IsHolidayAlt(state State, date time.Time) bool {
for _, holidayAlt := range holidayAlts {
if holidayAlt.Jurisdiction != state {
continue
}
location, err := time.LoadLocation(holidayAlt.Timezone)
if err != nil {
continue
}
// convert time in the timezone
timeWithTimezone := date.In(location)
y := timeWithTimezone.Year()
m := timeWithTimezone.Month()
d := timeWithTimezone.Day()
wd := timeWithTimezone.Weekday()
// check month
if holidayAlt.Month != m {
continue
}
// check weekday
if holidayAlt.Weekday != wd {
continue
}
// check date
firstDayOfTheMonth := time.Date(y, m, 1, 0, 0, 0, 0, time.UTC)
firstWeekdayOfTheMonth := int(firstDayOfTheMonth.Weekday())
diffOfTheDays := int(wd) - firstWeekdayOfTheMonth
if diffOfTheDays < 0 {
diffOfTheDays += 7
}
if d != firstDayOfTheMonth.Add(time.Duration(diffOfTheDays*24)*time.Hour).Add(time.Duration((holidayAlt.Week-1)*7*24)*time.Hour).Day() {
continue
}
return true
}
return false
}