Skip to content

cron job trigger on timezone based, daylightsaving taken care #40

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func task() {
fmt.Println("I am runnning task.")
}

func task1() {
fmt.Println("I am runnning task1.")
}

func taskWithParams(a int, b string) {
fmt.Println(a, b)
}
Expand All @@ -46,9 +50,9 @@ func main() {
gocron.Every(1).Monday().Do(task)
gocron.Every(1).Thursday().Do(task)

// function At() take a string like 'hour:min'
gocron.Every(1).Day().At("10:30").Do(task)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is still valuable to have this line here as an example of what you can use.
It isn't always clear what you do need and don't need when you're learning a new language.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. It can be retained.

gocron.Every(1).Monday().At("18:30").Do(task)
// function At() take a string like 'hour:min:sec' ->Add your time here
gocron.Every(1).Day().Zone("EST").At("12:34:37").Do(task1)
gocron.Every(1).Monday().At("18:30:00").Do(task)

// remove, clear and next_run
_, time := gocron.NextRun()
Expand Down
10 changes: 7 additions & 3 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ func task() {
fmt.Println("I am runnning task.")
}

func task1() {
fmt.Println("I am runnning task1.")
}

func taskWithParams(a int, b string) {
fmt.Println(a, b)
}
Expand All @@ -32,9 +36,9 @@ func main() {
gocron.Every(1).Monday().Do(task)
gocron.Every(1).Thursday().Do(task)

// function At() take a string like 'hour:min'
gocron.Every(1).Day().At("10:30").Do(task)
gocron.Every(1).Monday().At("18:30").Do(task)
// function At() take a string like 'hour:min:sec' ->Add your time here
gocron.Every(1).Day().Zone("EST").At("12:34:37").Do(task1)
gocron.Every(1).Monday().At("18:30:00").Do(task)

// remove, clear and next_run
_, time := gocron.NextRun()
Expand Down
29 changes: 22 additions & 7 deletions gocron.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ type Job struct {

// Map for function and params of function
fparams map[string]([]interface{})

//diff time zone for each timer
timeZone *time.Location
}

// Create a new job with the time interval.
Expand All @@ -76,6 +79,7 @@ func NewJob(intervel uint64) *Job {
time.Sunday,
make(map[string]interface{}),
make(map[string]([]interface{})),
time.UTC,
}
}

Expand All @@ -97,7 +101,7 @@ func (j *Job) run() (result []reflect.Value, err error) {
in[k] = reflect.ValueOf(param)
}
result = f.Call(in)
j.lastRun = time.Now()
j.lastRun = time.Now().In(j.timeZone)
j.scheduleNextRun()
return
}
Expand Down Expand Up @@ -128,27 +132,28 @@ func (j *Job) Do(jobFun interface{}, params ...interface{}) {
func (j *Job) At(t string) *Job {
hour := int((t[0]-'0')*10 + (t[1] - '0'))
min := int((t[3]-'0')*10 + (t[4] - '0'))
if hour < 0 || hour > 23 || min < 0 || min > 59 {
sec := int((t[6]-'0')*10 + (t[7] - '0'))
if hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 || sec > 59 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not handle leap seconds.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate a bit? I suppose leap second is beyond scope of gocron lib as the job gets triggered when at specified time in seconds. After the leap second, since time resets back and job is already triggered, so no issues.
Apologies for responding after years. :)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The library doesn't support winter/summer time changes that happen twice per year causing trigger to be missed completely once per year or triggered at wrong hour, leap seconds is surely something much less important than that.

panic("time format error.")
}
// time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
mock := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), int(hour), int(min), 0, 0, loc)
mock := time.Date(time.Now().In(j.timeZone).Year(), time.Now().In(j.timeZone).Month(), time.Now().In(j.timeZone).Day(), int(hour), int(min), int(sec), 0, j.timeZone)

if j.unit == "days" {
if time.Now().After(mock) {
j.lastRun = mock
} else {
j.lastRun = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-1, hour, min, 0, 0, loc)
j.lastRun = time.Date(time.Now().In(j.timeZone).AddDate(0, 0, -1).Year(), time.Now().In(j.timeZone).AddDate(0, 0, -1).Month(), time.Now().In(j.timeZone).AddDate(0, 0, -1).Day(), hour, min, sec, 0, j.timeZone)
}
} else if j.unit == "weeks" {
if time.Now().After(mock) {
i := mock.Weekday() - j.startDay
if i < 0 {
i = 7 + i
}
j.lastRun = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-int(i), hour, min, 0, 0, loc)
j.lastRun = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-int(i), hour, min, sec, 0, j.timeZone)
} else {
j.lastRun = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-7, hour, min, 0, 0, loc)
j.lastRun = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-7, hour, min, sec, 0, j.timeZone)
}
}
return j
Expand All @@ -162,7 +167,7 @@ func (j *Job) scheduleNextRun() {
if i < 0 {
i = 7 + i
}
j.lastRun = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-int(i), 0, 0, 0, 0, loc)
j.lastRun = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-int(i), 0, 0, 0, 0, j.timeZone)

} else {
j.lastRun = time.Now()
Expand Down Expand Up @@ -193,6 +198,16 @@ func (j *Job) scheduleNextRun() {
}
}

// Set timezone for timer
func (j *Job) Zone(timeZone string) *Job {
recivedTimeZone, err := time.LoadLocation(timeZone)
if err != nil {
panic("time zone format error.")
}
j.timeZone = recivedTimeZone
return j
}

// the follow functions set the job's unit with seconds,minutes,hours...

// Set the unit with second
Expand Down