-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhours.go
313 lines (286 loc) · 7.66 KB
/
hours.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package yext
import (
"fmt"
"strconv"
"strings"
"time"
)
const (
HoursClosedAllWeek = "1:closed,2:closed,3:closed,4:closed,5:closed,6:closed,7:closed"
HoursOpen24Hours = "00:00:00:00"
HoursClosed = "closed"
)
type Weekday int
// Following the documentation of the Yext API,
// the indexing of days begins at 1 (with Sunday)
// and ends with 7 (Saturday)
// http://developer.yext.com/docs/api-reference/
const (
Sunday Weekday = iota + 1
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
)
func (w Weekday) ToString() string {
switch w {
case Sunday:
return "Sunday"
case Monday:
return "Monday"
case Tuesday:
return "Tuesday"
case Wednesday:
return "Wednesday"
case Thursday:
return "Thursday"
case Friday:
return "Friday"
case Saturday:
return "Saturday"
}
return "Unknown"
}
type HoursHelper struct {
Sunday []string
Monday []string
Tuesday []string
Wednesday []string
Thursday []string
Friday []string
Saturday []string
}
func HoursHelperFromString(str string) (*HoursHelper, error) {
var (
hoursHelper = &HoursHelper{}
hoursForDays = strings.Split(str, ",")
)
if len(str) == 0 {
return hoursHelper, nil
}
for _, hoursForDay := range hoursForDays {
weekday, hours, err := parseWeekdayAndHoursFromString(hoursForDay)
if err != nil {
return nil, err
}
hoursHelper.AppendHours(weekday, hours)
}
for weekday, hours := range hoursHelper.ToMap() {
if hours == nil {
hoursHelper.SetClosed(weekday)
}
}
return hoursHelper, nil
}
func MustHoursHelperFromString(str string) *HoursHelper {
hoursHelper, err := HoursHelperFromString(str)
if err != nil {
panic(err)
}
return hoursHelper
}
func (h *HoursHelper) SetHours(weekday Weekday, hours []string) {
switch weekday {
case Sunday:
h.Sunday = hours
case Monday:
h.Monday = hours
case Tuesday:
h.Tuesday = hours
case Wednesday:
h.Wednesday = hours
case Thursday:
h.Thursday = hours
case Friday:
h.Friday = hours
case Saturday:
h.Saturday = hours
}
}
func (h *HoursHelper) AppendHours(weekday Weekday, hours string) {
switch weekday {
case Sunday:
h.Sunday = append(h.Sunday, hours)
case Monday:
h.Monday = append(h.Monday, hours)
case Tuesday:
h.Tuesday = append(h.Tuesday, hours)
case Wednesday:
h.Wednesday = append(h.Wednesday, hours)
case Thursday:
h.Thursday = append(h.Thursday, hours)
case Friday:
h.Friday = append(h.Friday, hours)
case Saturday:
h.Saturday = append(h.Saturday, hours)
}
}
func (h *HoursHelper) SetClosed(weekday Weekday) {
h.SetHours(weekday, []string{HoursClosed})
}
func (h *HoursHelper) SetUnspecified(weekday Weekday) {
h.SetHours(weekday, nil)
}
func (h *HoursHelper) SetOpen24Hours(weekday Weekday) {
h.SetHours(weekday, []string{HoursOpen24Hours})
}
func (h *HoursHelper) GetHours(weekday Weekday) []string {
switch weekday {
case Sunday:
return h.Sunday
case Monday:
return h.Monday
case Tuesday:
return h.Tuesday
case Wednesday:
return h.Wednesday
case Thursday:
return h.Thursday
case Friday:
return h.Friday
case Saturday:
return h.Saturday
}
return nil
}
func (h *HoursHelper) Serialize() string {
if h.HoursAreAllUnspecified() {
return ""
}
var days = []string{
h.SerializeDay(Sunday),
h.SerializeDay(Monday),
h.SerializeDay(Tuesday),
h.SerializeDay(Wednesday),
h.SerializeDay(Thursday),
h.SerializeDay(Friday),
h.SerializeDay(Saturday),
}
return strings.Join(days, ",")
}
func (h *HoursHelper) SerializeDay(weekday Weekday) string {
if h.HoursAreAllUnspecified() {
return ""
}
var hoursStrings = []string{}
if h.GetHours(weekday) == nil || len(h.GetHours(weekday)) == 0 {
return fmt.Sprintf("%d:%s", weekday, HoursClosed)
}
for _, hours := range h.GetHours(weekday) {
hoursStrings = append(hoursStrings, fmt.Sprintf("%d:%s", weekday, hours))
}
return strings.Join(hoursStrings, ",")
}
func (h *HoursHelper) ToStringSlice() ([]string, error) {
var hoursStringSlice = make([][]string, 7)
for i := range hoursStringSlice {
weekday := Weekday(i + 1)
if h.HoursAreClosed(weekday) {
hoursStringSlice[i] = []string{"Closed"}
} else if h.HoursAreOpen24Hours(weekday) {
hoursStringSlice[i] = []string{"24hr"}
} else {
hours := h.GetHours(weekday)
for _, h := range hours {
open, close, err := ParseOpenAndCloseHoursFromString(h)
if err != nil {
return nil, err
}
if open, err = ConvertBetweenFormats(open, "15:04", "3:04pm"); err != nil {
return nil, err
}
if close, err = ConvertBetweenFormats(close, "15:04", "3:04pm"); err != nil {
return nil, err
}
hoursStringSlice[i] = append(hoursStringSlice[i], fmt.Sprintf("%s - %s", open, close))
}
}
}
var hoursSlice = make([]string, 7)
for i, h := range hoursStringSlice {
hoursSlice[i] = strings.Join(h, ",")
}
return hoursSlice, nil
}
func (h *HoursHelper) MustToStringSlice() []string {
hoursStringSlice, err := h.ToStringSlice()
if err != nil {
panic(err)
}
return hoursStringSlice
}
func parseWeekdayAndHoursFromString(str string) (Weekday, string, error) {
if len(str) == 0 {
return -1, "", fmt.Errorf("Error parsing weekday and hours from string: string has 0 length")
}
hoursParts := strings.Split(str, ":")
if len(hoursParts) == 0 {
return -1, "", fmt.Errorf("Error parsing weekday and hours from string: string in unexpectd format")
}
weekdayInt, err := strconv.Atoi(hoursParts[0])
if err != nil {
return -1, "", fmt.Errorf("Error parsing weekday hours from string; unable to convert index to num: %s", err)
}
return Weekday(weekdayInt), strings.Join(hoursParts[1:], ":"), nil
}
func ParseOpenAndCloseHoursFromString(hours string) (string, string, error) {
if strings.Contains(hours, ":") {
parts := strings.Split(hours, ":")
if len(parts) == 4 {
return fmt.Sprintf("%s:%s", parts[0], parts[1]), fmt.Sprintf("%s:%s", parts[2], parts[3]), nil
} else if len(parts) == 5 {
return fmt.Sprintf("%s:%s", parts[1], parts[2]), fmt.Sprintf("%s:%s", parts[3], parts[4]), nil
}
}
return "", "", fmt.Errorf("Error parsing open and close hours from string %s: Unexpected format", hours)
}
func (h HoursHelper) ToMap() map[Weekday][]string {
return map[Weekday][]string{
Sunday: h.Sunday,
Monday: h.Monday,
Tuesday: h.Tuesday,
Wednesday: h.Wednesday,
Thursday: h.Thursday,
Friday: h.Friday,
Saturday: h.Saturday,
}
}
func (h *HoursHelper) HoursAreAllUnspecified() bool {
for _, hours := range h.ToMap() {
if hours != nil {
return false
}
}
return true
}
func (h *HoursHelper) HoursAreUnspecified(weekday Weekday) bool {
return h.GetHours(weekday) == nil
}
func (h *HoursHelper) HoursAreClosed(weekday Weekday) bool {
var hours = h.GetHours(weekday)
return hours != nil && len(hours) == 1 && hours[0] == HoursClosed
}
func (h *HoursHelper) HoursAreOpen24Hours(weekday Weekday) bool {
var hours = h.GetHours(weekday)
return hours != nil && len(hours) == 1 && hours[0] == HoursOpen24Hours
}
func ParseAndFormatHours(tFormat string, openHours string, closeHours string) (string, error) {
openTime, err := time.Parse(tFormat, openHours)
if err != nil {
return "", fmt.Errorf("Error parsing hours %s with format %s: %s", openHours, tFormat, err)
}
closeTime, err := time.Parse(tFormat, closeHours)
if err != nil {
return "", fmt.Errorf("Error parsing hours %s with format %s: %s", closeHours, tFormat, err)
}
return fmt.Sprintf("%s:%s", openTime.Format("15:04"), closeTime.Format("15:04")), nil
}
func ConvertBetweenFormats(hours string, convertFromFormat string, convertToFormat string) (string, error) {
t, err := time.Parse(convertFromFormat, hours)
if err != nil {
return "", fmt.Errorf("Hours %s was not in expected format %s: %s", hours, convertFromFormat, err)
}
return t.Format(convertToFormat), nil
}