forked from robfig/cron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.go
More file actions
199 lines (167 loc) · 3.46 KB
/
clock.go
File metadata and controls
199 lines (167 loc) · 3.46 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
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
package cron
import (
"time"
"github.com/banbox/bntp"
)
type Timer interface {
Stop() bool
Reset(d time.Duration) bool
Chan() <-chan time.Time
}
type FireableTimer interface {
Timer
Fire()
}
type Clock interface {
Now() time.Time
Timer(time.Time) Timer
SetTime(time.Time) bool
}
type DefaultClock struct {
location *time.Location
}
func NewDefaultClock(location *time.Location) *DefaultClock {
if location == nil {
location = time.Local
}
return &DefaultClock{
location: location,
}
}
func (c *DefaultClock) Now() time.Time {
return time.Now().In(c.location)
}
func (c *DefaultClock) Timer(t time.Time) Timer {
return NewStdTimer(time.NewTimer(t.Sub(c.Now())))
}
func (c *DefaultClock) SetTime(t time.Time) bool {
return false
}
type StdTimer struct {
*time.Timer
}
// NewStdTimer creates a Timer with system clock.
func NewStdTimer(t *time.Timer) *StdTimer {
return &StdTimer{
Timer: t,
}
}
func (t *StdTimer) Chan() <-chan time.Time {
return t.C
}
type CustomTimer struct {
*time.Timer
clock Clock
expireAt time.Time
c chan time.Time
}
// NewCustomTimer creates a Timer with custom clock.
func NewCustomTimer(d time.Duration, clock Clock) *CustomTimer {
ct := &CustomTimer{
Timer: time.NewTimer(d),
clock: clock,
expireAt: clock.Now().Add(d),
c: make(chan time.Time, 1),
}
go func() {
<-ct.Timer.C
ct.c <- ct.expireAt
}()
return ct
}
func (t *CustomTimer) Chan() <-chan time.Time {
return t.c
}
func (t *CustomTimer) Stop() bool {
return t.Timer.Stop()
}
func (t *CustomTimer) Reset(d time.Duration) bool {
// Reset the underlying timer
active := t.Timer.Reset(d)
// Update the expiration time based on current clock time
t.expireAt = t.clock.Now().Add(d)
// Start a new goroutine to handle the reset timer
go func() {
<-t.Timer.C
t.c <- t.expireAt
}()
return active
}
// NtpClock 实现 Clock 接口,使用 bntp 作为时间源
type NtpClock struct {
location *time.Location
}
// NewNtpClock LangGlobal, LangZhCN, LangZhHK, LangZhTW, LangJaJP, LangKoKr, LangZhSg, LangNone(default)
func NewNtpClock(location *time.Location, code string) *NtpClock {
if code != "" {
bntp.LangCode = code
}
return &NtpClock{
location: location,
}
}
func (c *NtpClock) Now() time.Time {
return bntp.Now().In(c.location)
}
func (c *NtpClock) Timer(t time.Time) Timer {
duration := t.Sub(c.Now())
return NewCustomTimer(duration, c)
}
func (c *NtpClock) SetTime(t time.Time) bool {
// bntp clock cannot be set
return false
}
type FakeTimer struct {
clock *FakeClock
expire time.Time
C chan time.Time
fired bool
}
func (t *FakeTimer) Stop() bool {
if t.fired {
return false
}
t.fired = true
return true
}
func (t *FakeTimer) Chan() <-chan time.Time {
return t.C
}
func (t *FakeTimer) Reset(d time.Duration) bool {
if t.fired {
return false
}
t.expire = t.clock.Now().Add(d)
return true
}
func (t *FakeTimer) Fire() {
if t.fired {
panic("timer already fired")
}
t.fired = true
t.C <- t.expire
}
type FakeClock struct {
location *time.Location
current time.Time
}
func NewFakeClock(location *time.Location, current time.Time) *FakeClock {
return &FakeClock{
location: location,
current: current.In(location),
}
}
func (c *FakeClock) Now() time.Time {
return c.current.In(c.location)
}
func (c *FakeClock) Timer(t time.Time) Timer {
return &FakeTimer{
clock: c,
expire: t,
C: make(chan time.Time),
}
}
func (c *FakeClock) SetTime(t time.Time) bool {
c.current = t
return true
}