Documentation
¶
Overview ¶
Package gocron provides primitives to parse a Cron expression and iterate over the activation times.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Iterator ¶ added in v0.1.4
type Iterator struct {
// contains filtered or unexported fields
}
type Schedule ¶
type Schedule struct {
// contains filtered or unexported fields
}
Schedule is a representation of a Cron expression.
func MustParse ¶ added in v0.1.7
MustParse returns a schedule from the Cron expression and panics in case of error.
func Parse ¶
Parse returns a schedule from the Cron expression and returns an error if the syntax is not supported or incorrect.
func (Schedule) Next ¶
Next returns a time after the given argument, but never equals to it. A zero time is returned when none can be found.
Example (EveryFifteenSeconds) ¶
package main import ( "fmt" "time" "github.com/Gilthoniel/gocron" ) func main() { schedule := gocron.MustParse("*/15 * * * ? *") next := schedule.Next(time.Date(2023, time.June, 4, 0, 0, 0, 0, time.UTC)) fmt.Println(next) next = schedule.Next(next) fmt.Println(next) }
Output: 2023-06-04 00:00:15 +0000 UTC 2023-06-04 00:00:30 +0000 UTC
Example (EveryLastFridayOfTheMonthAtMidnight) ¶
package main import ( "fmt" "time" "github.com/Gilthoniel/gocron" ) func main() { schedule := gocron.MustParse("0 0 0 ? * 5L") next := schedule.Next(time.Date(2023, time.June, 4, 0, 0, 0, 0, time.UTC)) fmt.Println(next) next = schedule.Next(next) fmt.Println(next) }
Output: 2023-06-30 00:00:00 +0000 UTC 2023-07-28 00:00:00 +0000 UTC
Example (UsingTimezone) ¶
package main import ( "fmt" "time" "github.com/Gilthoniel/gocron" ) func main() { schedule := gocron.MustParse("*/15 * * * * *") next := schedule.Next(time.Date(2023, time.June, 4, 0, 0, 0, 0, time.FixedZone("CEST", 120))) fmt.Println(next) next = schedule.Next(next) fmt.Println(next) }
Output: 2023-06-04 00:00:15 +0002 CEST 2023-06-04 00:00:30 +0002 CEST
func (Schedule) Preceding ¶ added in v1.0.0
Preceding returns an iterator that will iterate over the actiation times of the Cron expression of the schedule backwards from the given time.
func (Schedule) Previous ¶ added in v0.1.7
Previous returns a time before the given argument, but never equals it. A zero time is returned when none can be found.
func (Schedule) Upcoming ¶ added in v0.1.4
Upcoming returns an iterator that will iterate over the activation times of the Cron expression of the schedule forwards from the given time.
Example (EveryLastSundayOfAprilAtThreePM) ¶
package main import ( "fmt" "time" "github.com/Gilthoniel/gocron" ) func main() { schedule := gocron.MustParse("0 0 15 ? 4 0L") iter := schedule.Upcoming(time.Date(2023, time.June, 4, 0, 0, 0, 0, time.UTC)) for i := 0; i < 5 && iter.HasNext(); i++ { next := iter.Next() fmt.Println(next) } }
Output: 2024-04-28 15:00:00 +0000 UTC 2025-04-27 15:00:00 +0000 UTC 2026-04-26 15:00:00 +0000 UTC 2027-04-25 15:00:00 +0000 UTC 2028-04-30 15:00:00 +0000 UTC
Example (EverySecondToLastDayOfEveryTwoMonths) ¶
package main import ( "fmt" "time" "github.com/Gilthoniel/gocron" ) func main() { schedule := gocron.MustParse("0 0 0 L-2 */2 ?") iter := schedule.Upcoming(time.Date(2023, time.June, 4, 0, 0, 0, 0, time.UTC)) for i := 0; i < 5 && iter.HasNext(); i++ { next := iter.Next() fmt.Println(next) } }
Output: 2023-07-30 00:00:00 +0000 UTC 2023-09-29 00:00:00 +0000 UTC 2023-11-29 00:00:00 +0000 UTC 2024-01-30 00:00:00 +0000 UTC 2024-03-30 00:00:00 +0000 UTC
Example (EveryThirdThursdayOfEachMonth) ¶
package main import ( "fmt" "time" "github.com/Gilthoniel/gocron" ) func main() { schedule := gocron.MustParse("0 0 0 ? * 4#3") iter := schedule.Upcoming(time.Date(2023, time.June, 4, 0, 0, 0, 0, time.UTC)) for i := 0; i < 5 && iter.HasNext(); i++ { next := iter.Next() fmt.Println(next) } }
Output: 2023-06-15 00:00:00 +0000 UTC 2023-07-20 00:00:00 +0000 UTC 2023-08-17 00:00:00 +0000 UTC 2023-09-21 00:00:00 +0000 UTC 2023-10-19 00:00:00 +0000 UTC
type TimeUnit ¶
type TimeUnit interface { // Next returns the next iteration of a schedule and `true` when valid, // otherwise it returns a time after `next` and `false`. Next(next time.Time) (time.Time, bool) // Previous returns the previous iteration of a schedule and `true` when // valid, otherwise it returns a time before `prev` and `false`. Previous(prev time.Time) (time.Time, bool) }
TimeUnit represents a single part of a Cron expression.
type TimeUnitError ¶
type TimeUnitError struct {
// contains filtered or unexported fields
}
TimeUnitError is an error returned when a time unit of a Cron expression is malformed.
func (TimeUnitError) Error ¶
func (e TimeUnitError) Error() string
func (TimeUnitError) Is ¶
func (e TimeUnitError) Is(err error) bool
func (TimeUnitError) Kind ¶ added in v0.1.4
func (e TimeUnitError) Kind() TimeUnitKind
Kind returns the time unit kind of the error (e.g. seconds).
type TimeUnitKind ¶
type TimeUnitKind int
const ( Seconds TimeUnitKind = iota Minutes Hours Days Months WeekDays Years )
func (TimeUnitKind) String ¶
func (k TimeUnitKind) String() string