-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathschedule.go
251 lines (215 loc) · 6.16 KB
/
schedule.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2017 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package timeutil
import (
"fmt"
"math/rand"
"regexp"
"strconv"
"strings"
"time"
)
var validTime = regexp.MustCompile(`^([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])$`)
type TimeOfDay struct {
Hour int
Minute int
}
func (t TimeOfDay) String() string {
return fmt.Sprintf("%02d:%02d", t.Hour, t.Minute)
}
// ParseTime parses a string that contains hour:minute and returns
// an TimeOfDay type or an error
func ParseTime(s string) (t TimeOfDay, err error) {
m := validTime.FindStringSubmatch(s)
if len(m) == 0 {
return t, fmt.Errorf("cannot parse %q", s)
}
t.Hour, err = strconv.Atoi(m[1])
if err != nil {
return t, fmt.Errorf("cannot parse %q: %s", m[1], err)
}
t.Minute, err = strconv.Atoi(m[2])
if err != nil {
return t, fmt.Errorf("cannot parse %q: %s", m[2], err)
}
return t, nil
}
// Schedule defines a start and end time and an optional weekday in which
// events should run.
type Schedule struct {
Start TimeOfDay
End TimeOfDay
Weekday string
}
func (sched *Schedule) String() string {
if sched.Weekday == "" {
return fmt.Sprintf("%s-%s", sched.Start, sched.End)
}
return fmt.Sprintf("%s@%s-%s", sched.Weekday, sched.Start, sched.End)
}
func (sched *Schedule) Next(last time.Time) (start, end time.Time) {
now := timeNow()
wd := time.Weekday(weekdayMap[sched.Weekday])
t := last
for {
a := time.Date(t.Year(), t.Month(), t.Day(), sched.Start.Hour, sched.Start.Minute, 0, 0, time.Local)
b := time.Date(t.Year(), t.Month(), t.Day(), sched.End.Hour, sched.End.Minute, 0, 0, time.Local)
// not using AddDate() here as this can panic() if no
// location is set
t = t.Add(24 * time.Hour)
// we have not hit the right day yet
if sched.Weekday != "" && a.Weekday() != wd {
continue
}
// same inteval as last update, move forward
if (last.Equal(a) || last.After(a)) && (last.Equal(b) || last.Before(b)) {
continue
}
if b.Before(now) {
continue
}
return a, b
}
}
func randDur(a, b time.Time) time.Duration {
dur := b.Sub(a)
if dur > 5*time.Minute {
// doing it this way we still spread really small windows about
dur -= 5 * time.Minute
}
if dur <= 0 {
// avoid panic'ing (even if things are probably messed up)
return 0
}
return time.Duration(rand.Int63n(int64(dur)))
}
var (
timeNow = time.Now
// FIMXE: pass in as a parameter for next
maxDuration = 14 * 24 * time.Hour
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// Next will return the duration until a random time in the next
// schedule window.
func Next(schedule []*Schedule, last time.Time) time.Duration {
now := timeNow()
a := last.Add(maxDuration)
b := a.Add(1 * time.Hour)
for _, sched := range schedule {
start, end := sched.Next(last)
if start.Before(a) {
a = start
b = end
}
}
if a.Before(now) {
return 0
}
when := a.Sub(now) + randDur(a, b)
return when
}
var weekdayMap = map[string]int{
"sun": 0,
"mon": 1,
"tue": 2,
"wed": 3,
"thu": 4,
"fri": 5,
"sat": 6,
}
// parseWeekday gets an input like "mon@9:00-11:00" or "9:00-11:00"
// and extracts the weekday of that schedule string (which can be
// empty). It returns the remainder of the string, the weekday
// and an error.
func parseWeekday(s string) (weekday, rest string, err error) {
if !strings.Contains(s, "@") {
return "", s, nil
}
s = strings.ToLower(s)
l := strings.SplitN(s, "@", 2)
weekday = l[0]
_, ok := weekdayMap[weekday]
if !ok {
return "", "", fmt.Errorf(`cannot parse %q, want "mon", "tue", etc`, l[0])
}
rest = l[1]
return weekday, rest, nil
}
// parseTimeInterval gets an input like "9:00-11:00"
// and extracts the start and end of that schedule string and
// returns them and any errors.
func parseTimeInterval(s string) (start, end TimeOfDay, err error) {
if strings.Contains(s, "@") {
return start, end, fmt.Errorf("cannot parse %q: contains invalid @", s)
}
l := strings.SplitN(s, "-", 2)
if len(l) != 2 {
return start, end, fmt.Errorf("cannot parse %q: not a valid interval", s)
}
start, err = ParseTime(l[0])
if err != nil {
return start, end, fmt.Errorf("cannot parse %q: not a valid time", l[0])
}
end, err = ParseTime(l[1])
if err != nil {
return start, end, fmt.Errorf("cannot parse %q: not a valid time", l[1])
}
if !(start.Hour <= end.Hour && start.Minute <= end.Minute) {
return start, end, fmt.Errorf("cannot parse %q: time in an interval cannot go backwards", s)
}
return start, end, nil
}
// parseSingleSchedule parses a schedule string like "mon@9:00-11:00" or
// "9:00-11:00" and returns a Schedule struct and an error.
func parseSingleSchedule(s string) (*Schedule, error) {
weekday, rest, err := parseWeekday(s)
if err != nil {
return nil, err
}
start, end, err := parseTimeInterval(rest)
if err != nil {
return nil, err
}
return &Schedule{
Weekday: weekday,
Start: start,
End: end,
}, nil
}
// ParseSchedule takes a schedule string in the form of:
//
// 9:00-15:00 (every day between 9am and 3pm)
// 9:00-15:00/21:00-22:00 (every day between 9am,5pm and 9pm,10pm)
// thu@9:00-15:00 (only Thursday between 9am and 3pm)
// fri@9:00-11:00/mon@13:00-15:00 (only Friday between 9am and 3pm and Monday between 1pm and 3pm)
// fri@9:00-11:00/13:00-15:00 (only Friday between 9am and 3pm and every day between 1pm and 3pm)
//
// and returns a list of Schedule types or an error
func ParseSchedule(scheduleSpec string) ([]*Schedule, error) {
var schedule []*Schedule
for _, s := range strings.Split(scheduleSpec, "/") {
sched, err := parseSingleSchedule(s)
if err != nil {
return nil, err
}
schedule = append(schedule, sched)
}
return schedule, nil
}