Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5f7cf87

Browse files
committed
schedule: add methods to fetch tz and cron string separately
1 parent50ad2f8 commit5f7cf87

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

‎coderd/autobuild/schedule/schedule.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ var defaultParser = cron.NewParser(parserFormat)
3434
// us_sched, _ := schedule.Weekly("CRON_TZ=US/Central 30 9 1-5")
3535
// fmt.Println(sched.Next(time.Now()).Format(time.RFC3339))
3636
// // Output: 2022-04-04T14:30:00Z
37-
funcWeekly(specstring) (*Schedule,error) {
38-
iferr:=validateWeeklySpec(spec);err!=nil {
37+
funcWeekly(rawstring) (*Schedule,error) {
38+
iferr:=validateWeeklySpec(raw);err!=nil {
3939
returnnil,xerrors.Errorf("validate weekly schedule: %w",err)
4040
}
4141

42-
specSched,err:=defaultParser.Parse(spec)
42+
specSched,err:=defaultParser.Parse(raw)
4343
iferr!=nil {
4444
returnnil,xerrors.Errorf("parse schedule: %w",err)
4545
}
@@ -49,9 +49,18 @@ func Weekly(spec string) (*Schedule, error) {
4949
returnnil,xerrors.Errorf("expected *cron.SpecSchedule but got %T",specSched)
5050
}
5151

52+
tz:="UTC"
53+
cron:=raw
54+
ifstrings.HasPrefix(raw,"CRON_TZ=") {
55+
tz=strings.TrimPrefix(strings.Fields(raw)[0],"CRON_TZ=")
56+
cron=strings.Join(strings.Fields(raw)[1:]," ")
57+
}
58+
5259
cronSched:=&Schedule{
5360
sched:schedule,
54-
spec:spec,
61+
raw:raw,
62+
tz:tz,
63+
cron:cron,
5564
}
5665
returncronSched,nil
5766
}
@@ -61,12 +70,25 @@ func Weekly(spec string) (*Schedule, error) {
6170
typeSchedulestruct {
6271
sched*cron.SpecSchedule
6372
// XXX: there isn't any nice way for robfig/cron to serialize
64-
specstring
73+
rawstring
74+
tzstring
75+
cronstring
6576
}
6677

6778
// String serializes the schedule to its original human-friendly format.
6879
func (sSchedule)String()string {
69-
returns.spec
80+
returns.raw
81+
}
82+
83+
// Timezone returns the timezone for the schedule.
84+
func (sSchedule)Timezone()string {
85+
returns.tz
86+
}
87+
88+
// Cron returns the cron spec for the schedule with the leading CRON_TZ
89+
// stripped, if present.
90+
func (sSchedule)Cron()string {
91+
returns.cron
7092
}
7193

7294
// Next returns the next time in the schedule relative to t.

‎coderd/autobuild/schedule/schedule_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,26 @@ func Test_Weekly(t *testing.T) {
1717
at time.Time
1818
expectedNext time.Time
1919
expectedErrorstring
20+
expectedCronstring
21+
expectedTzstring
2022
}{
2123
{
2224
name:"with timezone",
2325
spec:"CRON_TZ=US/Central 30 9 * * 1-5",
2426
at:time.Date(2022,4,1,14,29,0,0,time.UTC),
2527
expectedNext:time.Date(2022,4,1,14,30,0,0,time.UTC),
2628
expectedError:"",
29+
expectedCron:"30 9 * * 1-5",
30+
expectedTz:"US/Central",
2731
},
2832
{
2933
name:"without timezone",
3034
spec:"30 9 * * 1-5",
3135
at:time.Date(2022,4,1,9,29,0,0,time.Local),
3236
expectedNext:time.Date(2022,4,1,9,30,0,0,time.Local),
3337
expectedError:"",
38+
expectedCron:"30 9 * * 1-5",
39+
expectedTz:"UTC",
3440
},
3541
{
3642
name:"invalid schedule",
@@ -86,6 +92,8 @@ func Test_Weekly(t *testing.T) {
8692
require.NoError(t,err)
8793
require.Equal(t,testCase.expectedNext,nextTime)
8894
require.Equal(t,testCase.spec,actual.String())
95+
require.Equal(t,testCase.expectedCron,actual.Cron())
96+
require.Equal(t,testCase.expectedTz,actual.Timezone())
8997
}else {
9098
require.EqualError(t,err,testCase.expectedError)
9199
require.Nil(t,actual)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp