forked from chneau/openhours
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openhours.go
292 lines (267 loc) · 6.92 KB
/
openhours.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
package openhours
import (
"errors"
"fmt"
"sort"
"strconv"
"strings"
"time"
)
var (
weekDays = map[string]int{"su": 0, "mo": 1, "tu": 2, "we": 3, "th": 4, "fr": 5, "sa": 6}
// Errors
ErrInvalidFormat error = errors.New("invalid format")
)
// OpenHours ...
type OpenHours []time.Time
func newDate(day, hour, min, sec, nsec int, loc *time.Location) time.Time {
return time.Date(2017, 1, day, hour, min, sec, nsec, loc)
}
func newDateFromTime(t time.Time) time.Time {
return newDate(int(t.Weekday()), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
}
// Match returns true if the time t is in the open hours
func (o OpenHours) Match(t time.Time) bool {
t = newDateFromTime(t)
i := o.matchIndex(t)
return i%2 == 1
}
// matchIndex returns the index of the next open hour
func (o OpenHours) matchIndex(t time.Time) int {
i := 0
for ; i < len(o); i++ {
if o[i].After(t) {
break
}
}
return i
}
// NextDur returns true if t is in the open hours and the duration until it closes
// else it returns false if t is in the closed hours and the duration until it opens
func (o OpenHours) NextDur(t time.Time) (bool, time.Duration) {
current := newDateFromTime(t)
i := o.matchIndex(current)
isOpen := i%2 == 1 // uneven -> next time is a closing time
if i == len(o) { // end of week, wrap around
i = 0
}
next := o[i]
if current.After(next) { // we wrapped, set days to end of week
next = next.AddDate(0, 0, 7)
}
return isOpen, tzDiff(next, current, t)
}
// tzDiff calculate diff between a and b and add it to t, taking in account eventual tz changes
func tzDiff(a, b, t time.Time) time.Duration {
diff := a.Sub(b)
_, offset := t.Zone()
_, newOffset := t.Add(diff).Zone()
return diff + time.Duration(offset-newOffset)*time.Second
}
// When returns the date where the duration can be done in one go during open hours
func (o OpenHours) When(t time.Time, d time.Duration) *time.Time {
x := newDateFromTime(t)
i := o.matchIndex(x)
var found *time.Time
if i%2 == 1 {
newO := x.Add(d)
// log.Println(x, newO, o[i], newO.Before(o[i]) || newO.Equal(o[i]), i, o)
if newO.Before(o[i]) || newO.Equal(o[i]) {
found = &x
} else {
i += 2
}
} else {
i++
}
for max := i + len(o); i < max && found == nil; i += 2 {
newI := i % len(o)
newO := o[newI-1].Add(d)
if newO.Before(o[newI]) || newO.Equal(o[newI]) {
found = &o[newI-1]
}
}
if found == nil {
return found
}
if x.After(*found) {
z := found.AddDate(0, 0, 7)
found = &z
}
f := t.Add(tzDiff(*found, x, t))
return &f
}
// NextDate uses nextDur to gives the date of interest
func (o OpenHours) NextDate(t time.Time) (bool, time.Time) {
b, dur := o.NextDur(t)
return b, t.Add(dur)
}
func (o OpenHours) Add(from, to time.Time) OpenHours {
o = append(o, newDateFromTime(from), newDateFromTime(to))
o = merge(o)
return o
}
var weekdays = map[int]string{0: "Sunday", 1: "Monday", 2: "Tuesday", 3: "Wednesday", 4: "Thursday", 5: "Friday", 6: "Saturday"}
func (o OpenHours) String() []string {
str := []string{}
if len(o) == 0 {
return str
}
for i := 1; i <= len(o)-1; i += 2 {
str = append(str, fmt.Sprintf("%s %s - %s", weekdays[o[i-1].Day()], o[i-1].Format("15:04"), o[i].Format("15:04")))
}
return str
}
func cleanStr(str string) string {
clean := strings.TrimSpace(str)
clean = strings.Join(strings.Fields(clean), " ")
clean = strings.ToLower(clean)
clean = strings.Replace(clean, " ,", ",", -1)
clean = strings.Replace(clean, ", ", ",", -1)
return clean
}
func simplifyDays(str string) []int {
simple := []int{}
days := map[int]struct{}{}
for _, str := range strings.Split(str, ",") {
switch len(str) {
case 2: // "mo"
if v, exist := weekDays[str]; exist {
days[v] = struct{}{}
}
continue
case 5: // "tu-fr"
strs := strings.Split(str, "-")
from, exist := weekDays[strs[0]]
if !exist {
continue
}
to, exist := weekDays[strs[1]]
if !exist {
continue
}
if to < from { // circular lookup
to += 7
}
for i := from; i <= to; i++ {
days[i%7] = struct{}{}
}
continue
}
}
for i := range days {
simple = append(simple, i)
}
sort.Ints(simple)
return simple
}
func simplifyTime(str string) (int, int, int) {
hour, min := 0, 0
strs := strings.Split(str, ":")
if len(strs) < 2 || len(strs) > 3 {
return 0, 0, 0
}
hour, _ = strconv.Atoi(strs[0])
min, _ = strconv.Atoi(strs[1])
var sec int
if len(strs) == 3 {
sec, _ = strconv.Atoi(strs[2])
}
if hour > 24 {
hour = hour % 24
}
if hour > 24 || hour < 0 || min > 59 || min < 0 || sec > 59 || sec < 0 || (hour == 24 && min > 0 || hour == 24 && sec > 0) {
return 0, 0, 0
}
return hour, min, sec
}
func new(str string, loc *time.Location) (OpenHours, error) {
if loc == nil {
loc = time.UTC
}
o := []time.Time{}
if len(str) > 0 && str[len(str)-1] == ';' {
str = str[:len(str)-1]
}
if str == "" {
str = "su-sa 00:00-24:00"
}
for _, str := range strings.Split(cleanStr(str), ";") {
strs := strings.Fields(str)
if len(strs) < 2 {
return nil, ErrInvalidFormat
}
days := simplifyDays(strs[0])
for _, str := range strings.Split(strs[1], ",") {
times := strings.Split(str, "-")
if len(times) != 2 {
return nil, ErrInvalidFormat
}
hourFrom, minFrom, secFrom := simplifyTime(times[0])
hourTo, minTo, secTo := simplifyTime(times[1])
for _, day := range days {
fromDate := newDate(day, hourFrom, minFrom, secFrom, 0, loc)
if hourFrom > hourTo { // closing after midnight
day++
}
o = append(o, fromDate, newDate(day, hourTo, minTo, secTo, 0, loc))
}
}
}
return o, nil
}
func merge4(o ...time.Time) (bool, []time.Time) {
for i := 0; i < len(o)-1; i++ {
if o[i].After(o[i+1]) || o[i].Equal(o[i+1]) {
sort.Slice(o, func(i, j int) bool {
return o[i].Before(o[j])
})
return true, []time.Time{o[0], o[len(o)-1]}
}
}
return false, nil
}
func merge(o []time.Time) []time.Time {
sort.SliceStable(o, func(i, j int) bool {
if o[i].Day() == o[j].Day() {
return o[i].Hour() < o[j].Hour()
}
return o[i].Day() < o[j].Day()
})
for i := 0; i < len(o); i += 2 {
for j := i + 2; j < len(o); j += 2 {
perform, res := merge4(o[i], o[i+1], o[j], o[j+1])
if !perform {
continue
}
o[i], o[i+1] = res[0], res[1]
o = append(o[:j], o[j+2:]...)
i -= 2
break
}
}
return o
}
// New returns a new instance of an openhours.
// If loc is nil, UTC is used.
func New(str string, loc *time.Location) (OpenHours, error) {
o, err := new(str, loc)
return merge(o), err
}
// NewMust returns a new instance of an openhours or panics on error
// If loc is nil, UTC is used.
func NewMust(str string, loc *time.Location) OpenHours {
o, err := new(str, loc)
if err != nil {
panic(err)
}
return merge(o)
}
// NewLocal returns a new instance of an openhours with local timezone
func NewLocal(str string) (OpenHours, error) {
return New(str, time.Local)
}
// NewUTC returns a new instance of an openhours with UTC timezone
func NewUTC(str string) (OpenHours, error) {
return New(str, time.UTC)
}