Files
2025-09-22 00:41:57 +08:00

23 lines
670 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utils
import (
"time"
"github.com/robfig/cron/v3"
)
// GetNextCronTime 根据传入的 Cron 表达式计算下一次执行的时间。
// 它使用兼容大多数 Cron 实现的标准解析器。
// 如果 Cron 表达式无效,它将返回一个错误。
func GetNextCronTime(cronExpression string) (time.Time, error) {
// cron.ParseStandard() 返回一个支持标准5位或6位带秒格式的解析器。
schedule, err := cron.ParseStandard(cronExpression)
if err != nil {
return time.Time{}, err // 返回零值时间和错误
}
// 从当前时间计算下一次执行时间
nextTime := schedule.Next(time.Now())
return nextTime, nil
}