From 3002e1731498f4b8f867e428bf91c95538288580 Mon Sep 17 00:00:00 2001 From: lou Date: Tue, 1 Mar 2022 18:32:29 +0800 Subject: [PATCH] support different tz in one scheduler Signed-off-by: lou --- scheduler.go | 7 ++++++- scheduler_test.go | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/scheduler.go b/scheduler.go index b92da946..7d3bb721 100644 --- a/scheduler.go +++ b/scheduler.go @@ -1138,7 +1138,12 @@ func (s *Scheduler) cron(cronExpression string, withSeconds bool) *Scheduler { job = s.getCurrentJob() } - withLocation := fmt.Sprintf("CRON_TZ=%s %s", s.location.String(), cronExpression) + var withLocation string + if strings.HasPrefix(cronExpression, "TZ=") || strings.HasPrefix(cronExpression, "CRON_TZ=") { + withLocation = cronExpression + } else { + withLocation = fmt.Sprintf("CRON_TZ=%s %s", s.location.String(), cronExpression) + } var ( cronSchedule cron.Schedule diff --git a/scheduler_test.go b/scheduler_test.go index fe5d3d4e..18c79777 100644 --- a/scheduler_test.go +++ b/scheduler_test.go @@ -1673,6 +1673,7 @@ func TestScheduler_Cron(t *testing.T) { {"every minute in range, monday thru friday", "15-30 * * * 1-5", ft.onNow(time.UTC).Add(15 * time.Minute), nil}, {"at every minute past every hour from 1 through 5 on every day-of-week from Monday through Friday.", "* 1-5 * * 1-5", ft.onNow(time.UTC).Add(13 * time.Hour), nil}, {"hourly", "@hourly", ft.onNow(time.UTC).Add(1 * time.Hour), nil}, + {"every day 1am in shanghai", "CRON_TZ=Asia/Shanghai 0 1 * * *", ft.onNow(time.UTC).Add(5 * time.Hour), nil}, {"bad expression", "bad", time.Time{}, wrapOrError(fmt.Errorf("expected exactly 5 fields, found 1: [bad]"), ErrCronParseFailure)}, }