Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
13 changes: 13 additions & 0 deletions _example/location.go
Original file line number Diff line number Diff line change
@@ -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)
}
42 changes: 42 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strconv"
"strings"
"time"
)

type (
Expand Down Expand Up @@ -55,6 +56,44 @@ 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 {
panic(err)
}

schdule, err := Parse(expr)
if err != nil {
panic(err)
}

schdule.Location = loc
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 {
return nil, err
}

schdule, err := Parse(expr)
if err != nil {
return nil, err
}

schdule.Location = loc
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 {
Expand All @@ -64,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 {
Expand Down
21 changes: 17 additions & 4 deletions schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ type (
Schedule struct {
Minute, Hour, Dom, Month, Dow bitset

// TODO: support the timezone option
// Location *time.Location
Location *time.Location
}
)

// 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 {
loc = s.Location
}

origLoc := t.Location()
t.In(loc)
Comment on lines +18 to +23
Copy link
Member Author

Choose a reason for hiding this comment

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

Change the location of t when it evaluates the next time, and revert the location when it returns.


added := false

Expand Down Expand Up @@ -88,11 +94,18 @@ L:
}
}

return t
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 {
loc = s.Location
}

origLoc := t.Location()
t.In(loc)

subtracted := false

Expand Down Expand Up @@ -167,7 +180,7 @@ L:
}
}

return t
return t.In(origLoc)
}

// dayMatches returns true if the schedule's day-of-week and day-of-month
Expand Down