-
Notifications
You must be signed in to change notification settings - Fork 343
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -76,6 +79,7 @@ func NewJob(intervel uint64) *Job { | |
time.Sunday, | ||
make(map[string]interface{}), | ||
make(map[string]([]interface{})), | ||
time.UTC, | ||
} | ||
} | ||
|
||
|
@@ -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 | ||
} | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not handle leap seconds. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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() | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.