1- // This library implements a cron spec parser and runner.  See the README for 
2- // more details. 
31package  cron
42
53import  (
@@ -19,6 +17,7 @@ type Cron struct {
1917	snapshot  chan  []* Entry 
2018	running   bool 
2119	ErrorLog  * log.Logger 
20+ 	location  * time.Location 
2221}
2322
2423// Job is an interface for submitted cron jobs. 
@@ -74,15 +73,21 @@ func (s byTime) Less(i, j int) bool {
7473	return  s [i ].Next .Before (s [j ].Next )
7574}
7675
77- // New returns a new Cron job runner. 
76+ // New returns a new Cron job runner, in the Local time zone . 
7877func  New () * Cron  {
78+ 	return  NewWithLocation (time .Now ().Location ())
79+ }
80+ 
81+ // NewWithLocation returns a new Cron job runner. 
82+ func  NewWithLocation (location  * time.Location ) * Cron  {
7983	return  & Cron {
8084		entries :  nil ,
8185		add :      make (chan  * Entry ),
8286		stop :     make (chan  struct {}),
8387		snapshot : make (chan  []* Entry ),
8488		running :  false ,
8589		ErrorLog : nil ,
90+ 		location : location ,
8691	}
8792}
8893
@@ -131,6 +136,11 @@ func (c *Cron) Entries() []*Entry {
131136	return  c .entrySnapshot ()
132137}
133138
139+ // Location gets the time zone location 
140+ func  (c  * Cron ) Location () * time.Location  {
141+ 	return  c .location 
142+ }
143+ 
134144// Start the cron scheduler in its own go-routine, or no-op if already started. 
135145func  (c  * Cron ) Start () {
136146	if  c .running  {
@@ -140,6 +150,15 @@ func (c *Cron) Start() {
140150	go  c .run ()
141151}
142152
153+ // Run the cron scheduler, or no-op if already running. 
154+ func  (c  * Cron ) Run () {
155+ 	if  c .running  {
156+ 		return 
157+ 	}
158+ 	c .running  =  true 
159+ 	c .run ()
160+ }
161+ 
143162func  (c  * Cron ) runWithRecovery (j  Job ) {
144163	defer  func () {
145164		if  r  :=  recover (); r  !=  nil  {
@@ -156,7 +175,7 @@ func (c *Cron) runWithRecovery(j Job) {
156175// access to the 'running' state variable. 
157176func  (c  * Cron ) run () {
158177	// Figure out the next activation times for each entry. 
159- 	now  :=  time .Now ().Local ( )
178+ 	now  :=  time .Now ().In ( c . location )
160179	for  _ , entry  :=  range  c .entries  {
161180		entry .Next  =  entry .Schedule .Next (now )
162181	}
@@ -177,6 +196,7 @@ func (c *Cron) run() {
177196		timer  :=  time .NewTimer (effective .Sub (now ))
178197		select  {
179198		case  now  =  <- timer .C :
199+ 			now  =  now .In (c .location )
180200			// Run every entry whose next time was this effective time. 
181201			for  _ , e  :=  range  c .entries  {
182202				if  e .Next  !=  effective  {
@@ -191,7 +211,7 @@ func (c *Cron) run() {
191211
192212		case  newEntry  :=  <- c .add :
193213			c .entries  =  append (c .entries , newEntry )
194- 			newEntry .Next  =  newEntry .Schedule .Next (time .Now ().Local ( ))
214+ 			newEntry .Next  =  newEntry .Schedule .Next (time .Now ().In ( c . location ))
195215
196216		case  <- c .snapshot :
197217			c .snapshot  <-  c .entrySnapshot ()
@@ -202,7 +222,7 @@ func (c *Cron) run() {
202222		}
203223
204224		// 'now' should be updated after newEntry and snapshot cases. 
205- 		now  =  time .Now ().Local ( )
225+ 		now  =  time .Now ().In ( c . location )
206226		timer .Stop ()
207227	}
208228}
0 commit comments