-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule.go
More file actions
86 lines (66 loc) · 1.85 KB
/
schedule.go
File metadata and controls
86 lines (66 loc) · 1.85 KB
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
package hardloop
import "time"
type ScheduleGroup struct {
StartSchedules []Schedule
StopSchedules []Schedule
}
func NewSchedule(startSpec, endSpec []string) (*ScheduleGroup, error) {
startSchedules := make([]Schedule, 0, len(startSpec))
stopSchedules := make([]Schedule, 0, len(endSpec))
for _, spec := range startSpec {
startSchedule, err := ParseStandard(spec)
if err != nil {
return nil, err
}
startSchedules = append(startSchedules, startSchedule)
}
for _, spec := range endSpec {
stopSchedule, err := ParseStandard(spec)
if err != nil {
return nil, err
}
stopSchedules = append(stopSchedules, stopSchedule)
}
return &ScheduleGroup{
StartSchedules: startSchedules,
StopSchedules: stopSchedules,
}, nil
}
// getStartTime if return nil, start now.
func (l *ScheduleGroup) getStartTime(now time.Time) (*time.Time, error) {
nextStart := FindNext(l.StartSchedules, now)
if nextStart.IsZero() {
return nil, errTimeNotSet
}
return &nextStart, nil
}
// getStopTime if return nil, stop now.
func (l *ScheduleGroup) getStopTime(now time.Time) (*time.Time, error) {
prevStop := FindPrev(l.StopSchedules, now)
if prevStop.IsZero() {
// stop the ScheduleGroup
return nil, errTimeNotSet
}
prevStart := FindPrev(l.StartSchedules, now)
// if prevStop is after prevStart, then we should stop the loop
if !prevStart.IsZero() && prevStop.After(prevStart) {
// stop the loop
return nil, nil
}
nextStop := FindNext(l.StopSchedules, now)
if nextStop.IsZero() {
// stop the loop
return nil, errTimeNotSet
}
return &nextStop, nil
}
// NextStartTime returns the next start time.
// - If it should be start now than it returns nil.
func (l *ScheduleGroup) NextStartTime(now time.Time) *time.Time {
stopTime, _ := l.getStopTime(now)
if stopTime != nil {
return nil
}
startTime, _ := l.getStartTime(now)
return startTime
}