From 150bb34e51ad31d1f64f68adebc2f349f1a8c26e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=B7=AF=E8=B7=AF?= Date: Wed, 4 Mar 2026 09:12:55 +0800 Subject: [PATCH] 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. --- pkg/cron/service.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/cron/service.go b/pkg/cron/service.go index 6962041c1..6a74369ac 100644 --- a/pkg/cron/service.go +++ b/pkg/cron/service.go @@ -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)