改回cron解析五位表达式

This commit is contained in:
2025-09-22 00:41:57 +08:00
parent d6eaf23289
commit 0a8e6793ef
3 changed files with 5 additions and 80 deletions

View File

@@ -6,18 +6,12 @@ import (
"github.com/robfig/cron/v3"
)
// cronParserWithSeconds 是一个自定义的 cron 解析器,它包含了秒字段。
// 这允许解析6部分的 cron 表达式(带秒),并处理 '?' 通配符。
var cronParserWithSeconds = cron.NewParser(
cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor,
)
// GetNextCronTime 根据传入的 Cron 表达式计算下一次执行的时间。
// 它使用一个支持秒级精度和'?'通配符的解析器。
// 它使用兼容大多数 Cron 实现的标准解析器。
// 如果 Cron 表达式无效,它将返回一个错误。
func GetNextCronTime(cronExpression string) (time.Time, error) {
// 使用自定义的解析器来解析表达式
schedule, err := cronParserWithSeconds.Parse(cronExpression)
// cron.ParseStandard() 返回一个支持标准5位或6位带秒格式的解析器。
schedule, err := cron.ParseStandard(cronExpression)
if err != nil {
return time.Time{}, err // 返回零值时间和错误
}