-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedule.go
55 lines (45 loc) · 985 Bytes
/
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
package cherryTimeWheel
import (
"time"
)
// Scheduler determines the execution plan of a task.
type Scheduler interface {
// Next returns the next execution time after the given (previous) time.
// It will return a zero time if no next time is scheduled.
//
// All times must be UTC.
Next(time.Time) time.Time
}
type EverySchedule struct {
Interval time.Duration
}
func (s *EverySchedule) Next(prev time.Time) time.Time {
return prev.Add(s.Interval)
}
type FixedDateSchedule struct {
Hour, Minute, Second int
}
func (s *FixedDateSchedule) Next(prev time.Time) time.Time {
hour := prev.Hour()
if s.Hour >= 0 {
hour = s.Hour
}
fixedTime := time.Date(
prev.Year(),
prev.Month(),
prev.Day(),
hour,
s.Minute,
s.Second,
0,
prev.Location(),
)
remain := fixedTime.UnixNano() - prev.UnixNano()
if remain > 0 {
return prev.Add(time.Duration(remain))
}
if s.Hour >= 0 {
return fixedTime.Add(24 * time.Hour)
}
return fixedTime.Add(time.Hour)
}