From fb9035c0947268fe579ee124caf1d258b1f719b3 Mon Sep 17 00:00:00 2001 From: noah Date: Sat, 25 Dec 2021 18:00:29 +0900 Subject: [PATCH 1/3] Add new functions parsing with location --- parser.go | 31 +++++++++++++++++++++++++++++++ schedule.go | 13 +++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/parser.go b/parser.go index cfdfe15..3cb7e28 100644 --- a/parser.go +++ b/parser.go @@ -4,6 +4,7 @@ import ( "fmt" "strconv" "strings" + "time" ) type ( @@ -55,6 +56,36 @@ var ( } ) +func MustParseInLocation(expr string, locName string) *Schedule { + loc, err := time.LoadLocation(locName) + if err != nil { + panic(err) + } + + schdule, err := Parse(expr) + if err != nil { + panic(err) + } + + schdule.Location = loc + return schdule +} + +func ParseInLocation(expr string, locName string) (*Schedule, error) { + loc, err := time.LoadLocation(locName) + if err != nil { + return nil, err + } + + schdule, err := Parse(expr) + if err != nil { + return nil, err + } + + schdule.Location = loc + return schdule, nil +} + func MustParse(expr string) *Schedule { s, err := Parse(expr) if err != nil { diff --git a/schedule.go b/schedule.go index 492f677..d4e33ea 100644 --- a/schedule.go +++ b/schedule.go @@ -8,13 +8,17 @@ type ( Schedule struct { Minute, Hour, Dom, Month, Dow bitset - // TODO: support the timezone option - // Location *time.Location + Location *time.Location } ) func (s *Schedule) Next(t time.Time) time.Time { loc := time.UTC + if s.Location != nil { + loc = s.Location + } + + t.In(loc) added := false @@ -93,6 +97,11 @@ L: func (s *Schedule) Prev(t time.Time) time.Time { loc := time.UTC + if s.Location != nil { + loc = s.Location + } + + t.In(loc) subtracted := false From a8bbaab6d3876b17c9aba54f37d8c9803df770e5 Mon Sep 17 00:00:00 2001 From: noah Date: Sat, 25 Dec 2021 18:22:43 +0900 Subject: [PATCH 2/3] Fix to revert into the orignal location --- _example/location.go | 13 +++++++++++++ schedule.go | 6 ++++-- 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 _example/location.go diff --git a/_example/location.go b/_example/location.go new file mode 100644 index 0000000..4dbe97d --- /dev/null +++ b/_example/location.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" + "time" + + "github.com/gitploy-io/cronexpr" +) + +func main() { + nextTime := cronexpr.MustParseInLocation("0 * * * *", "Asia/Seoul").Next(time.Now()) + fmt.Printf("Parse the cron expression in the KR timezone: %s", nextTime) +} diff --git a/schedule.go b/schedule.go index d4e33ea..438839a 100644 --- a/schedule.go +++ b/schedule.go @@ -18,6 +18,7 @@ func (s *Schedule) Next(t time.Time) time.Time { loc = s.Location } + origLoc := t.Location() t.In(loc) added := false @@ -92,7 +93,7 @@ L: } } - return t + return t.In(origLoc) } func (s *Schedule) Prev(t time.Time) time.Time { @@ -101,6 +102,7 @@ func (s *Schedule) Prev(t time.Time) time.Time { loc = s.Location } + origLoc := t.Location() t.In(loc) subtracted := false @@ -176,7 +178,7 @@ L: } } - return t + return t.In(origLoc) } // dayMatches returns true if the schedule's day-of-week and day-of-month From 750ed954a0cb5701b0e8a7d391ab113cb329871e Mon Sep 17 00:00:00 2001 From: noah Date: Sat, 25 Dec 2021 18:30:19 +0900 Subject: [PATCH 3/3] Add comments for functions --- README.md | 1 - parser.go | 11 +++++++++++ schedule.go | 2 ++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e40a365..d2e10d0 100644 --- a/README.md +++ b/README.md @@ -54,5 +54,4 @@ Hyphens define ranges. For example, 2000-2010 indicates every year between 2000 ## Details -* At this moment, the package supports only **UTC** timezone. * The return value of `Next` and `Prev` is zero if the pattern doesn't match in five years. diff --git a/parser.go b/parser.go index 3cb7e28..6bd29b8 100644 --- a/parser.go +++ b/parser.go @@ -56,6 +56,8 @@ var ( } ) +// MustParse returns the same result as Parse but it panic +// when something is wrong. func MustParseInLocation(expr string, locName string) *Schedule { loc, err := time.LoadLocation(locName) if err != nil { @@ -71,6 +73,10 @@ func MustParseInLocation(expr string, locName string) *Schedule { return schdule } +// ParseInLocation parse the expression in the location and +// returns a new schedule representing the given spec. +// It returns an error when loading the location is failed or +// the syntax of the expression is wrong. func ParseInLocation(expr string, locName string) (*Schedule, error) { loc, err := time.LoadLocation(locName) if err != nil { @@ -86,6 +92,8 @@ func ParseInLocation(expr string, locName string) (*Schedule, error) { return schdule, nil } +// MustParse returns the same result as Parse but it panic +// when the syntax of expression is wrong. func MustParse(expr string) *Schedule { s, err := Parse(expr) if err != nil { @@ -95,6 +103,9 @@ func MustParse(expr string) *Schedule { return s } +// Parse parses the expression and returns a new schedule representing the given spec. +// And the default location of a schedule is "UTC". +// It returns an error when the syntax of expression is wrong. func Parse(expr string) (*Schedule, error) { err := verifyExpr(expr) if err != nil { diff --git a/schedule.go b/schedule.go index 438839a..1bb45b4 100644 --- a/schedule.go +++ b/schedule.go @@ -12,6 +12,7 @@ type ( } ) +// Next returns the next time matched with the expression. func (s *Schedule) Next(t time.Time) time.Time { loc := time.UTC if s.Location != nil { @@ -96,6 +97,7 @@ L: return t.In(origLoc) } +// Next returns the previous time matched with the expression. func (s *Schedule) Prev(t time.Time) time.Time { loc := time.UTC if s.Location != nil {