Skip to content

Commit 5822783

Browse files
author
Noah Lee
authored
Merge pull request #1 from gitploy-io/custom-timezone
Support the customization of timezone
2 parents 106903c + 750ed95 commit 5822783

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,4 @@ Hyphens define ranges. For example, 2000-2010 indicates every year between 2000
5454

5555
## Details
5656

57-
* At this moment, the package supports only **UTC** timezone.
5857
* The return value of `Next` and `Prev` is zero if the pattern doesn't match in five years.

_example/location.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/gitploy-io/cronexpr"
8+
)
9+
10+
func main() {
11+
nextTime := cronexpr.MustParseInLocation("0 * * * *", "Asia/Seoul").Next(time.Now())
12+
fmt.Printf("Parse the cron expression in the KR timezone: %s", nextTime)
13+
}

parser.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strconv"
66
"strings"
7+
"time"
78
)
89

910
type (
@@ -55,6 +56,44 @@ var (
5556
}
5657
)
5758

59+
// MustParse returns the same result as Parse but it panic
60+
// when something is wrong.
61+
func MustParseInLocation(expr string, locName string) *Schedule {
62+
loc, err := time.LoadLocation(locName)
63+
if err != nil {
64+
panic(err)
65+
}
66+
67+
schdule, err := Parse(expr)
68+
if err != nil {
69+
panic(err)
70+
}
71+
72+
schdule.Location = loc
73+
return schdule
74+
}
75+
76+
// ParseInLocation parse the expression in the location and
77+
// returns a new schedule representing the given spec.
78+
// It returns an error when loading the location is failed or
79+
// the syntax of the expression is wrong.
80+
func ParseInLocation(expr string, locName string) (*Schedule, error) {
81+
loc, err := time.LoadLocation(locName)
82+
if err != nil {
83+
return nil, err
84+
}
85+
86+
schdule, err := Parse(expr)
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
schdule.Location = loc
92+
return schdule, nil
93+
}
94+
95+
// MustParse returns the same result as Parse but it panic
96+
// when the syntax of expression is wrong.
5897
func MustParse(expr string) *Schedule {
5998
s, err := Parse(expr)
6099
if err != nil {
@@ -64,6 +103,9 @@ func MustParse(expr string) *Schedule {
64103
return s
65104
}
66105

106+
// Parse parses the expression and returns a new schedule representing the given spec.
107+
// And the default location of a schedule is "UTC".
108+
// It returns an error when the syntax of expression is wrong.
67109
func Parse(expr string) (*Schedule, error) {
68110
err := verifyExpr(expr)
69111
if err != nil {

schedule.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ type (
88
Schedule struct {
99
Minute, Hour, Dom, Month, Dow bitset
1010

11-
// TODO: support the timezone option
12-
// Location *time.Location
11+
Location *time.Location
1312
}
1413
)
1514

15+
// Next returns the next time matched with the expression.
1616
func (s *Schedule) Next(t time.Time) time.Time {
1717
loc := time.UTC
18+
if s.Location != nil {
19+
loc = s.Location
20+
}
21+
22+
origLoc := t.Location()
23+
t.In(loc)
1824

1925
added := false
2026

@@ -88,11 +94,18 @@ L:
8894
}
8995
}
9096

91-
return t
97+
return t.In(origLoc)
9298
}
9399

100+
// Next returns the previous time matched with the expression.
94101
func (s *Schedule) Prev(t time.Time) time.Time {
95102
loc := time.UTC
103+
if s.Location != nil {
104+
loc = s.Location
105+
}
106+
107+
origLoc := t.Location()
108+
t.In(loc)
96109

97110
subtracted := false
98111

@@ -167,7 +180,7 @@ L:
167180
}
168181
}
169182

170-
return t
183+
return t.In(origLoc)
171184
}
172185

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

0 commit comments

Comments
 (0)