gocron/vendor/github.com/jakecoffman/cron/constantdelay.go

29 lines
997 B
Go
Raw Normal View History

2017-03-01 06:59:46 +00:00
package cron
import "time"
// ConstantDelaySchedule represents a simple recurring duty cycle, e.g. "Every 5 minutes".
// It does not support jobs more frequent than once a second.
type ConstantDelaySchedule struct {
Delay time.Duration
}
// Every returns a crontab Schedule that activates once every duration.
2017-04-13 09:35:59 +00:00
// Delays of less than a second are not supported (will panic).
2017-03-01 06:59:46 +00:00
// Any fields less than a Second are truncated.
func Every(duration time.Duration) ConstantDelaySchedule {
if duration < time.Second {
2017-04-13 09:35:59 +00:00
panic("cron/constantdelay: delays of less than a second are not supported: " +
duration.String())
2017-03-01 06:59:46 +00:00
}
return ConstantDelaySchedule{
Delay: duration - time.Duration(duration.Nanoseconds())%time.Second,
}
}
// Next returns the next time this should be run.
// This rounds so that the next activation time will be on the second.
func (schedule ConstantDelaySchedule) Next(t time.Time) time.Time {
return t.Add(schedule.Delay - time.Duration(t.Nanosecond())*time.Nanosecond)
}