@@ -19,6 +19,7 @@ type Cron struct {
19
19
snapshot chan []* Entry
20
20
running bool
21
21
ErrorLog * log.Logger
22
+ location * time.Location
22
23
}
23
24
24
25
// Job is an interface for submitted cron jobs.
@@ -69,15 +70,21 @@ func (s byTime) Less(i, j int) bool {
69
70
return s [i ].Next .Before (s [j ].Next )
70
71
}
71
72
72
- // New returns a new Cron job runner.
73
+ // New returns a new Cron job runner, in the Local time zone .
73
74
func New () * Cron {
75
+ return NewWithLocation (time .Now ().Location ())
76
+ }
77
+
78
+ // NewWithLocation returns a new Cron job runner.
79
+ func NewWithLocation (location * time.Location ) * Cron {
74
80
return & Cron {
75
81
entries : nil ,
76
82
add : make (chan * Entry ),
77
83
stop : make (chan struct {}),
78
84
snapshot : make (chan []* Entry ),
79
85
running : false ,
80
86
ErrorLog : nil ,
87
+ location : location ,
81
88
}
82
89
}
83
90
@@ -125,6 +132,11 @@ func (c *Cron) Entries() []*Entry {
125
132
return c .entrySnapshot ()
126
133
}
127
134
135
+ // Location gets the time zone location
136
+ func (c * Cron ) Location () * time.Location {
137
+ return c .location
138
+ }
139
+
128
140
// Start the cron scheduler in its own go-routine, or no-op if already started.
129
141
func (c * Cron ) Start () {
130
142
if c .running {
@@ -150,7 +162,7 @@ func (c *Cron) runWithRecovery(j Job) {
150
162
// access to the 'running' state variable.
151
163
func (c * Cron ) run () {
152
164
// Figure out the next activation times for each entry.
153
- now := time .Now ().Local ( )
165
+ now := time .Now ().In ( c . location )
154
166
for _ , entry := range c .entries {
155
167
entry .Next = entry .Schedule .Next (now )
156
168
}
@@ -184,7 +196,7 @@ func (c *Cron) run() {
184
196
185
197
case newEntry := <- c .add :
186
198
c .entries = append (c .entries , newEntry )
187
- newEntry .Next = newEntry .Schedule .Next (time .Now ().Local ( ))
199
+ newEntry .Next = newEntry .Schedule .Next (time .Now ().In ( c . location ))
188
200
189
201
case <- c .snapshot :
190
202
c .snapshot <- c .entrySnapshot ()
@@ -195,7 +207,7 @@ func (c *Cron) run() {
195
207
}
196
208
197
209
// 'now' should be updated after newEntry and snapshot cases.
198
- now = time .Now ().Local ( )
210
+ now = time .Now ().In ( c . location )
199
211
timer .Stop ()
200
212
}
201
213
}
0 commit comments