-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.go
226 lines (199 loc) · 5.61 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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"sort"
"strconv"
"strings"
"time"
)
type Schedule struct {
defaultRate int64
blocks []ScheduleBlock
}
type ScheduleBlock struct {
weekday time.Weekday
startHour int
startMinute int
endHour int
endMinute int
rate int64
}
func parseWeekday(s string) (time.Weekday, error) {
switch s {
case "mon", "monday":
return time.Monday, nil
case "tue", "tuesday":
return time.Tuesday, nil
case "wed", "wednesday":
return time.Wednesday, nil
case "thu", "thursday":
return time.Thursday, nil
case "fri", "friday":
return time.Friday, nil
case "sat", "saturday":
return time.Saturday, nil
case "sun", "sunday":
return time.Sunday, nil
default:
return -1, fmt.Errorf("invalid week day: %s", s)
}
}
func readSchedule(fn string) (*Schedule, error) {
file, err := os.Open(fn)
if err != nil {
return nil, err
}
defer file.Close()
var defaultRate int64
var blocks []ScheduleBlock
scanner := bufio.NewScanner(file)
lineNo := 0
for scanner.Scan() {
line := scanner.Text()
lineNo++
if len(line) == 0 || line[0] == '#' {
continue
}
if strings.HasPrefix(line, "default:") {
parts := strings.SplitN(line, ":", 2)
defaultRate, err = parseRate(strings.TrimSpace(parts[1]))
if err != nil {
return nil, err
}
continue
}
parts := strings.Split(line, ":")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid format on line %d (expected one colon)", lineNo)
}
temporalSpec := strings.Split(strings.TrimSpace(parts[0]), " ")
if len(temporalSpec) != 2 {
return nil, fmt.Errorf("invalid format on line %d (missing weekday or time spec)", lineNo)
}
weekdaySpec := strings.Split(temporalSpec[0], "-")
if len(weekdaySpec) > 2 {
return nil, fmt.Errorf("invalid format on line %d (too many '-' characters)", lineNo)
}
startWeekday, err := parseWeekday(weekdaySpec[0])
if err != nil {
return nil, err
}
weekdays := []time.Weekday{startWeekday}
if len(weekdaySpec) > 1 {
endWeekday, err := parseWeekday(weekdaySpec[1])
if err != nil {
return nil, err
}
if endWeekday < startWeekday {
endWeekday += 7
}
for i := startWeekday + 1; i <= endWeekday; i++ {
weekdays = append(weekdays, i%7)
}
}
timeRange := strings.Split(temporalSpec[1], "-")
if len(timeRange) != 2 {
return nil, fmt.Errorf("invalid format on line %d (bad time range)", lineNo)
}
if len(timeRange[0]) != 4 || len(timeRange[1]) != 4 {
return nil, fmt.Errorf("invalid format on line %d (bad time range). missing leading zero?", lineNo)
}
startHour, err := strconv.Atoi(timeRange[0][0:2])
if err != nil {
return nil, err
}
startMinute, err := strconv.Atoi(timeRange[0][2:4])
if err != nil {
return nil, err
}
endHour, err := strconv.Atoi(timeRange[1][0:2])
if err != nil {
return nil, err
}
endMinute, err := strconv.Atoi(timeRange[1][2:4])
if err != nil {
return nil, err
}
if startHour > 23 || startMinute > 59 ||
endHour > 23 || endMinute > 59 ||
endHour < startHour ||
(startHour == endHour && endMinute < startMinute) {
return nil, fmt.Errorf("invalid format on line %d (bad time spec)", lineNo)
}
rate, err := parseRate(strings.TrimSpace(parts[1]))
if err != nil {
return nil, err
}
for _, weekday := range weekdays {
blocks = append(blocks, ScheduleBlock{weekday, startHour, startMinute, endHour, endMinute, rate})
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
sort.Slice(blocks, func(i, j int) bool {
return blocks[i].weekday < blocks[j].weekday ||
(blocks[i].weekday == blocks[j].weekday &&
blocks[i].startHour < blocks[j].startHour) ||
(blocks[i].weekday == blocks[j].weekday &&
blocks[i].startHour == blocks[j].startHour &&
blocks[i].startMinute < blocks[j].startMinute)
})
if len(blocks) == 0 {
return nil, errors.New("schedule is empty")
} else if len(blocks) > 1 {
for i := 0; i < len(blocks)-1; i++ {
j := (i + 1)
if blocks[i].weekday != blocks[j].weekday {
continue
}
if blocks[i].endHour > blocks[j].startHour ||
(blocks[i].endHour == blocks[j].startHour && blocks[i].endMinute > blocks[j].startMinute) {
return nil, errors.New("time ranges are not allowed to overlap")
}
}
}
return &Schedule{defaultRate, blocks}, nil
}
func (s Schedule) next() ScheduleBlock {
now := time.Now()
var minBlock *ScheduleBlock
var minTimeUntil time.Duration
for i := range s.blocks {
block := s.blocks[i]
start, _ := block.next()
timeUntil := start.Sub(now)
if minBlock == nil || timeUntil < 0 || timeUntil < minTimeUntil {
minBlock = &block
minTimeUntil = timeUntil
}
}
return *minBlock
}
func (block ScheduleBlock) next() (time.Time, time.Time) {
now := time.Now()
today := now.Weekday()
days := int(block.weekday-today+7) % 7
t := now.AddDate(0, 0, days)
start := time.Date(t.Year(), t.Month(), t.Day(), block.startHour, block.startMinute, 0, 0, t.Location())
end := time.Date(t.Year(), t.Month(), t.Day(), block.endHour, block.endMinute, 0, 0, t.Location())
// Add a week if the time has already passed this week
// This also accounts for DST (the actual time may be different after constructing the time object) 😱
if end.Before(start) {
end = end.Add(time.Hour)
}
if now.After(end) {
t = t.AddDate(0, 0, 7)
start = time.Date(t.Year(), t.Month(), t.Day(), block.startHour, block.startMinute, 0, 0, t.Location())
end = time.Date(t.Year(), t.Month(), t.Day(), block.endHour, block.endMinute, 0, 0, t.Location())
}
return start, end
}
func (block ScheduleBlock) active() bool {
nextStart, _ := block.next()
now := time.Now()
return now.After(nextStart)
}