Skip to content

Commit fb9035c

Browse files
author
noah
committed
Add new functions parsing with location
1 parent 106903c commit fb9035c

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

parser.go

Lines changed: 31 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,36 @@ var (
5556
}
5657
)
5758

59+
func MustParseInLocation(expr string, locName string) *Schedule {
60+
loc, err := time.LoadLocation(locName)
61+
if err != nil {
62+
panic(err)
63+
}
64+
65+
schdule, err := Parse(expr)
66+
if err != nil {
67+
panic(err)
68+
}
69+
70+
schdule.Location = loc
71+
return schdule
72+
}
73+
74+
func ParseInLocation(expr string, locName string) (*Schedule, error) {
75+
loc, err := time.LoadLocation(locName)
76+
if err != nil {
77+
return nil, err
78+
}
79+
80+
schdule, err := Parse(expr)
81+
if err != nil {
82+
return nil, err
83+
}
84+
85+
schdule.Location = loc
86+
return schdule, nil
87+
}
88+
5889
func MustParse(expr string) *Schedule {
5990
s, err := Parse(expr)
6091
if err != nil {

schedule.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ 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

1615
func (s *Schedule) Next(t time.Time) time.Time {
1716
loc := time.UTC
17+
if s.Location != nil {
18+
loc = s.Location
19+
}
20+
21+
t.In(loc)
1822

1923
added := false
2024

@@ -93,6 +97,11 @@ L:
9397

9498
func (s *Schedule) Prev(t time.Time) time.Time {
9599
loc := time.UTC
100+
if s.Location != nil {
101+
loc = s.Location
102+
}
103+
104+
t.In(loc)
96105

97106
subtracted := false
98107

0 commit comments

Comments
 (0)