-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from kumparan/feature/add-GetCronNextAt
feature: add GetCronNextAt
- Loading branch information
Showing
5 changed files
with
71 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package utils | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/robfig/cron/v3" | ||
) | ||
|
||
var cronNextAtTimeFormat = "2006-01-02T15:04:05" | ||
|
||
// GetCronNextAt supports | ||
// - Standard crontab specs, e.g. "* * * * ?" | ||
// - Descriptors, e.g. "@midnight", "@every 1h30m" | ||
// if cron parsing error then return current time | ||
func GetCronNextAt(cronTab string) string { | ||
now := time.Now() | ||
var schedule, err = cron.ParseStandard(cronTab) | ||
if err != nil { | ||
return now.Format(cronNextAtTimeFormat) | ||
} | ||
|
||
return schedule.Next(now).Format(cronNextAtTimeFormat) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/agiledragon/gomonkey" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCron_GetCronNextAt(t *testing.T) { | ||
now, _ := time.Parse(time.RFC3339, "2010-01-02T15:00:00Z") | ||
patch := gomonkey.ApplyFunc(time.Now, func() time.Time { return now }) | ||
defer patch.Reset() | ||
|
||
assert.Equal(t, now.Format(cronNextAtTimeFormat), GetCronNextAt("* ngaco * cron nya ")) // wrong cron tab,return current time as next at | ||
assert.Equal(t, now.Add(1*time.Hour).Format(cronNextAtTimeFormat), GetCronNextAt("@hourly")) | ||
assert.Equal(t, now.Add(1*time.Minute).Format(cronNextAtTimeFormat), GetCronNextAt("*/1 * * * *")) | ||
assert.Equal(t, now.Add(1*time.Hour).Format(cronNextAtTimeFormat), GetCronNextAt("0 */1 * * *")) // every hour | ||
assert.Equal(t, now.Add(2*time.Hour).Format(cronNextAtTimeFormat), GetCronNextAt("0 17 */1 * *")) // every 17:00 | ||
assert.Equal(t, now.Add(30*time.Second).Format(cronNextAtTimeFormat), GetCronNextAt("@every 30s")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters