Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SetLocation method to crontab #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crontab.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Crontab struct {
ticker *time.Ticker
jobs []*job
sync.RWMutex
location *time.Location
}

// job in cron table
Expand Down Expand Up @@ -62,6 +63,13 @@ func new(t time.Duration) *Crontab {
return c
}

// SetLocation to run cron on specific timezone.
//
// Returns error if provided location is not valid
func (c *Crontab) SetLocation(loc *time.Location) {
c.location = loc
}

// AddJob to cron table
//
// Returns error if:
Expand Down Expand Up @@ -153,6 +161,10 @@ func (c *Crontab) RunAll() {

// RunScheduled jobs
func (c *Crontab) runScheduled(t time.Time) {
if c.location != nil {
t = t.In(c.location)
}

tick := getTick(t)
c.RLock()
defer c.RUnlock()
Expand Down
6 changes: 4 additions & 2 deletions crontab_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package crontab

import "testing"
import "time"
import (
"testing"
"time"
)

// TestSchedule parse the crontab syntax and compare number of target min/hour/days/month with expected ones
func TestSchedule(t *testing.T) {
Expand Down