From 193eaed00c49f9036c70c93d3449f98fbf49155c Mon Sep 17 00:00:00 2001 From: silves-xiang Date: Fri, 22 Aug 2025 10:28:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=97=B6=E9=97=B4=E5=A4=84=E7=90=86):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=AE=9A=E4=B9=89=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E5=87=BD=E6=95=B0=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 WithGetTimeFn 选项函数,允许用户自定义获取当前时间的逻辑 默认使用 DefaultGetTimeFn 返回当前系统时间 修改 now() 方法使用配置的 getTimeFn 获取时间 --- cron.go | 31 +++++++++++++++++++++---------- option.go | 7 +++++++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/cron.go b/cron.go index c7e91766..615a3d07 100644 --- a/cron.go +++ b/cron.go @@ -24,6 +24,7 @@ type Cron struct { parser ScheduleParser nextID EntryID jobWaiter sync.WaitGroup + getTimeFn GetTimeFn } // ScheduleParser is an interface for schedule spec parsers that return a Schedule @@ -93,21 +94,30 @@ func (s byTime) Less(i, j int) bool { return s[i].Next.Before(s[j].Next) } +// Get Time Fn, Provide for user to customize the get time function +type GetTimeFn func() time.Time + +// GetTime returns the current time in the cron's location. +// This can be overridden to mock time in tests. +var DefaultGetTimeFn = func() time.Time { + return time.Now() +} + // New returns a new Cron job runner, modified by the given options. // // Available Settings // -// Time Zone -// Description: The time zone in which schedules are interpreted -// Default: time.Local +// Time Zone +// Description: The time zone in which schedules are interpreted +// Default: time.Local // -// Parser -// Description: Parser converts cron spec strings into cron.Schedules. -// Default: Accepts this spec: https://en.wikipedia.org/wiki/Cron +// Parser +// Description: Parser converts cron spec strings into cron.Schedules. +// Default: Accepts this spec: https://en.wikipedia.org/wiki/Cron // -// Chain -// Description: Wrap submitted jobs to customize behavior. -// Default: A chain that recovers panics and logs them to stderr. +// Chain +// Description: Wrap submitted jobs to customize behavior. +// Default: A chain that recovers panics and logs them to stderr. // // See "cron.With*" to modify the default behavior. func New(opts ...Option) *Cron { @@ -123,6 +133,7 @@ func New(opts ...Option) *Cron { logger: DefaultLogger, location: time.Local, parser: standardParser, + getTimeFn: DefaultGetTimeFn, } for _, opt := range opts { opt(c) @@ -315,7 +326,7 @@ func (c *Cron) startJob(j Job) { // now returns current time in c location func (c *Cron) now() time.Time { - return time.Now().In(c.location) + return c.getTimeFn().In(c.location) } // Stop stops the cron scheduler if it is running; otherwise it does nothing. diff --git a/option.go b/option.go index 09e4278e..63980b82 100644 --- a/option.go +++ b/option.go @@ -43,3 +43,10 @@ func WithLogger(logger Logger) Option { c.logger = logger } } + +// WithGetTimeFn overrides the get time function used for interpreting job schedules. +func WithGetTimeFn(fn GetTimeFn) Option { + return func(c *Cron) { + c.getTimeFn = fn + } +}