fix(cron): apply timezone from schedule.TZ for cron expressions

CronSchedule has a TZ field but computeNextRun() ignores it, using
time.UnixMilli() which returns UTC. This causes cron expressions like
'0 9 * * *' (daily at 9am) to trigger at UTC 9:00 = CST 17:00.

Now loads schedule.TZ (defaulting to Asia/Shanghai when empty) and
converts the reference time to the target timezone before computing
the next tick with gronx.
This commit is contained in:
王路路 2026-03-04 09:12:55 +08:00
parent 6f5930624b
commit 150bb34e51

View file

@ -266,6 +266,14 @@ func (cs *CronService) computeNextRun(schedule *CronSchedule, nowMS int64) *int6
// Use gronx to calculate next run time
now := time.UnixMilli(nowMS)
// Apply timezone if specified, default to Asia/Shanghai
tz := schedule.TZ
if tz == "" {
tz = "Asia/Shanghai"
}
if loc, err := time.LoadLocation(tz); err == nil {
now = now.In(loc)
}
nextTime, err := gronx.NextTickAfter(schedule.Expr, now, false)
if err != nil {
log.Printf("[cron] failed to compute next run for expr '%s': %v", schedule.Expr, err)