Documentation ¶
Index ¶
- Constants
- func CeilDeltaDays(t1, t2 time.Time) int
- func CeilDeltaHours(t1, t2 time.Time) int
- func CeilDeltaMinutes(t1, t2 time.Time) int
- func Delta(t1, t2 time.Time) time.Duration
- func ExponentialBackoff(count, maxRetries int, baseDelay, maxDelay time.Duration, ...) time.Duration
- func FloorDeltaDays(t1, t2 time.Time) int
- func FloorDeltaHours(t1, t2 time.Time) int
- func FloorDeltaMinutes(t1, t2 time.Time) int
- func GetEndOfDay(t time.Time) time.Time
- func GetEndOfWeek(t time.Time, weekday time.Weekday) time.Time
- func GetMonthDays(t time.Time) int
- func GetNextMoment(now time.Time, hour, min, sec int) time.Time
- func GetRelativeEndOfDay(t time.Time, offsetDays int) time.Time
- func GetRelativeEndOfWeek(now time.Time, week time.Weekday, offsetWeeks int) time.Time
- func GetRelativeStartOfDay(t time.Time, offsetDays int) time.Time
- func GetRelativeStartOfWeek(now time.Time, week time.Weekday, offsetWeeks int) time.Time
- func GetRelativeTimeOfWeek(now time.Time, week time.Weekday, offsetWeeks int) time.Time
- func GetStartOfDay(t time.Time) time.Time
- func GetStartOfWeek(t time.Time, weekday time.Weekday) time.Time
- func IsMomentFuture(now time.Time, hour, min, sec int) bool
- func IsMomentPassed(now time.Time, hour, min, sec int) bool
- func IsSameDay(t1, t2 time.Time) bool
- func IsSameHour(t1, t2 time.Time) bool
- func IsSameMinute(t1, t2 time.Time) bool
- func IsSameMonth(t1, t2 time.Time) bool
- func IsSameSecond(t1, t2 time.Time) bool
- func IsSameWeek(t1, t2 time.Time) bool
- func IsSameYear(t1, t2 time.Time) bool
- func IsZero(t time.Time) bool
- func Max(t1, t2 time.Time) time.Time
- func Min(t1, t2 time.Time) time.Time
- func Now() time.Time
- func RegisterAfterTask(name string, after time.Duration, function interface{}, args ...interface{})
- func RegisterCronTask(name, expression string, function interface{}, args ...interface{}) error
- func RegisterDayMomentTask(name string, lastExecuted time.Time, offset time.Duration, hour, min, sec int, ...)
- func RegisterImmediateCronTask(name, expression string, function interface{}, args ...interface{}) error
- func RegisterRepeatedTask(name string, after, interval time.Duration, times int, function interface{}, ...)
- func RoundDeltaDays(t1, t2 time.Time) int
- func RoundDeltaHours(t1, t2 time.Time) int
- func RoundDeltaMinutes(t1, t2 time.Time) int
- func Since(t time.Time) time.Duration
- func SmallerFirst(t1, t2 time.Time) (time.Time, time.Time)
- func SmallerLast(t1, t2 time.Time) (time.Time, time.Time)
- func StandardExponentialBackoff(count, maxRetries int, baseDelay, maxDelay time.Duration) time.Duration
- func ToDuration[V constraints.Number](v V, unit ...time.Duration) time.Duration
- func ToDurationDay[V constraints.Number](v V) time.Duration
- func ToDurationHour[V constraints.Number](v V) time.Duration
- func ToDurationMinute[V constraints.Number](v V) time.Duration
- func ToDurationSecond[V constraints.Number](v V) time.Duration
- func ToDurationWeek[V constraints.Number](v V) time.Duration
- func UnregisterTask(name string)
- func Zero() time.Time
- type Adjuster
- type Period
- func NewPeriod(start, end time.Time) Period
- func NewPeriodWindow(t time.Time, size time.Duration) Period
- func NewPeriodWindowWeek(t time.Time) Period
- func NewPeriodWithDay(t time.Time, day int) Period
- func NewPeriodWithDayZero(t time.Time, day int) Period
- func NewPeriodWithHour(t time.Time, hour int) Period
- func NewPeriodWithMicrosecond(t time.Time, microsecond int) Period
- func NewPeriodWithMillisecond(t time.Time, millisecond int) Period
- func NewPeriodWithMinute(t time.Time, minute int) Period
- func NewPeriodWithNanosecond(t time.Time, nanosecond int) Period
- func NewPeriodWithSecond(t time.Time, second int) Period
- func NewPeriodWithTimeArray(times [2]time.Time) Period
- func (p Period) Days() int
- func (p Period) Duration() time.Duration
- func (p Period) End() time.Time
- func (p Period) Hours() int
- func (p Period) IsAfter(t time.Time) bool
- func (p Period) IsBefore(t time.Time) bool
- func (p Period) IsBetween(t time.Time) bool
- func (p Period) IsBetweenOrEqual(t time.Time) bool
- func (p Period) IsBetweenOrEqualPeriod(t Period) bool
- func (p Period) IsInvalid() bool
- func (p Period) IsOngoing(t time.Time) bool
- func (p Period) IsOverlap(t Period) bool
- func (p Period) IsZero() bool
- func (p Period) Microseconds() int
- func (p Period) Milliseconds() int
- func (p Period) Minutes() int
- func (p Period) Nanoseconds() int
- func (p Period) Seconds() int
- func (p Period) Start() time.Time
- type Scheduler
- func (s *Scheduler) Clear()
- func (s *Scheduler) Close()
- func (s *Scheduler) GetRegisteredTasks() []string
- func (s *Scheduler) RegisterAfterTask(name string, after time.Duration, function any, args ...any)
- func (s *Scheduler) RegisterCronTask(name, expression string, function any, args ...any) error
- func (s *Scheduler) RegisterDayMomentTask(name string, lastExecuted time.Time, offset time.Duration, hour, min, sec int, ...)
- func (s *Scheduler) RegisterImmediateCronTask(name, expression string, function any, args ...any) error
- func (s *Scheduler) RegisterRepeatedTask(name string, after, interval time.Duration, times int, function any, ...)
- func (s *Scheduler) SetExecutor(executor SchedulerExecutor)
- func (s *Scheduler) UnregisterTask(name string)
- type SchedulerExecutor
- type StateLine
- func (s *StateLine[State]) AddState(state State, t time.Time, onTrigger ...func()) *StateLine[State]
- func (s *StateLine[State]) AddTriggerToState(state State, onTrigger ...func()) *StateLine[State]
- func (s *StateLine[State]) Check(missingAllowed bool, states ...State) bool
- func (s *StateLine[State]) GetIndexByState(state State) int
- func (s *StateLine[State]) GetLastState() State
- func (s *StateLine[State]) GetMissingStates(states ...State) []State
- func (s *StateLine[State]) GetNextStateTimeByIndex(index int) time.Time
- func (s *StateLine[State]) GetNextTimeByState(state State) time.Time
- func (s *StateLine[State]) GetPrevStateTimeByIndex(index int) time.Time
- func (s *StateLine[State]) GetPrevTimeByState(state State) time.Time
- func (s *StateLine[State]) GetStateByIndex(index int) State
- func (s *StateLine[State]) GetStateByTime(t time.Time) State
- func (s *StateLine[State]) GetStateCount() int
- func (s *StateLine[State]) GetStateIndexByTime(t time.Time) int
- func (s *StateLine[State]) GetTimeByIndex(index int) time.Time
- func (s *StateLine[State]) GetTimeByState(state State) time.Time
- func (s *StateLine[State]) GetTriggerByIndex(index int) []func()
- func (s *StateLine[State]) GetTriggerByState(state State) []func()
- func (s *StateLine[State]) GetTriggerByTime(t time.Time) []func()
- func (s *StateLine[State]) HasState(state State) bool
- func (s *StateLine[State]) Iterate(handler func(index int, state State, t time.Time) bool)
- func (s *StateLine[State]) IterateReverse(handler func(index int, state State, t time.Time) bool)
- func (s *StateLine[State]) Move(d time.Duration) *StateLine[State]
- func (s *StateLine[State]) String() string
Constants ¶
const ( Nanosecond = time.Nanosecond Microsecond = time.Microsecond Millisecond = time.Millisecond Second = time.Second Minute = time.Minute Hour = time.Hour Day = Hour * 24 Week = Day * 7 )
const ( DefaultSchedulerTick = time.Millisecond * 10 DefaultSchedulerWheelSize = 10 )
const ( SchedulerForever = -1 // 无限循环 SchedulerOnce = 1 // 一次 SchedulerInstantly = 0 // 立刻 )
const (
BuiltInSchedulerWheelSize = 50
)
Variables ¶
This section is empty.
Functions ¶
func CeilDeltaMinutes ¶
CeilDeltaMinutes 计算两个时间之间的分钟数差异,并向上取整
func ExponentialBackoff ¶ added in v0.6.0
func ExponentialBackoff(count, maxRetries int, baseDelay, maxDelay time.Duration, multiplier, randomization float64) time.Duration
ExponentialBackoff 退避指数函数用于计算下一次重试将在多长时间后发生,当返回 -1 时表示不再重试
- count:当前重试次数
- maxRetries:最大重试次数,当为负数时表示无限重试
- baseDelay:基础延迟
- maxDelay:最大延迟
- multiplier:延迟时间的乘数,通常为 2
- randomization:延迟时间的随机化因子,通常为 0.5
func FloorDeltaHours ¶
FloorDeltaHours 计算两个时间之间的小时数差异,并向下取整
func FloorDeltaMinutes ¶
FloorDeltaMinutes 计算两个时间之间的分钟数差异,并向下取整
func GetEndOfWeek ¶
GetEndOfWeek 获取指定时间所在周的特定周的最后时刻,即 23:59:59
func GetNextMoment ¶
GetNextMoment 获取下一个指定时刻发生的时间。
func GetRelativeEndOfDay ¶
GetRelativeEndOfDay 获取相对于指定时间减去或加上指定天数后的当天结束时间
- offsetDays: 要偏移的天数,负数表示过去的某一天,正数表示未来的某一天
func GetRelativeEndOfWeek ¶
GetRelativeEndOfWeek 获取相对于当前时间的本周结束时间,以指定的星期作为一周的开始,并根据需要进行周数的偏移
- 该函数详细解释参考 GetRelativeEndOfWeek 函数,其中不同的是,该函数返回的是这一天最后一刻的时间,即 23:59:59
func GetRelativeStartOfDay ¶
GetRelativeStartOfDay 获取相对于指定时间减去或加上指定天数后的当天开始时间
- offsetDays: 要偏移的天数,负数表示过去的某一天,正数表示未来的某一天
func GetRelativeStartOfWeek ¶
GetRelativeStartOfWeek 获取相对于当前时间的本周开始时间,以指定的星期作为一周的开始,并根据需要进行周数的偏移
- now:当前时间
- week:以哪一天作为一周的开始
- offsetWeeks:要偏移的周数,正数表示向未来偏移,负数表示向过去偏移
该函数返回以指定星期作为一周的开始时间,然后根据偏移量进行周数偏移,得到相对于当前时间的周的开始时间
假设 week 为 time.Saturday 且 offsetWeeks 为 -1,则表示获取上周六的开始时间,一下情况中第一个时间为 now,第二个时间为函数返回值
- 2024-03-01 00:00:00 --相对时间-> 2024-02-24 00:00:00 --偏移时间--> 2024-02-17 00:00:00
- 2024-03-02 00:00:00 --相对时间-> 2024-02-24 00:00:00 --偏移时间--> 2024-02-17 00:00:00
- 2024-03-03 00:00:00 --相对时间-> 2024-03-02 00:00:00 --偏移时间--> 2024-02-24 00:00:00
func GetRelativeTimeOfWeek ¶
GetRelativeTimeOfWeek 获取相对于当前时间的本周指定星期的指定时刻,以指定的星期作为一周的开始,并根据需要进行周数的偏移
- 该函数详细解释参考 GetRelativeStartOfWeek 函数,其中不同的是,该函数返回的是这一天对应 now 的时间
func GetStartOfDay ¶
GetStartOfDay 获取指定时间的当天第一刻,即 00:00:00
func GetStartOfWeek ¶
GetStartOfWeek 获取指定时间所在周的特定周的开始时刻,即 00:00:00
func IsMomentFuture ¶
IsMomentFuture 检查指定时刻是否在未来
func IsMomentPassed ¶
IsMomentPassed 检查指定时刻是否已经过去
func RegisterAfterTask ¶
RegisterAfterTask 调用内置时间调度器 BuiltInScheduler 的 Scheduler.RegisterAfterTask 函数
func RegisterCronTask ¶
RegisterCronTask 调用内置时间调度器 BuiltInScheduler 的 Scheduler.RegisterCronTask 函数
func RegisterDayMomentTask ¶
func RegisterDayMomentTask(name string, lastExecuted time.Time, offset time.Duration, hour, min, sec int, function interface{}, args ...interface{})
RegisterDayMomentTask 调用内置时间调度器 BuiltInScheduler 的 Scheduler.RegisterDayMomentTask 函数
func RegisterImmediateCronTask ¶
func RegisterImmediateCronTask(name, expression string, function interface{}, args ...interface{}) error
RegisterImmediateCronTask 调用内置时间调度器 BuiltInScheduler 的 Scheduler.RegisterImmediateCronTask 函数
func RegisterRepeatedTask ¶
func RegisterRepeatedTask(name string, after, interval time.Duration, times int, function interface{}, args ...interface{})
RegisterRepeatedTask 调用内置时间调度器 BuiltInScheduler 的 Scheduler.RegisterRepeatedTask 函数
func RoundDeltaHours ¶
RoundDeltaHours 计算两个时间之间的小时数差异,并四舍五入
func RoundDeltaMinutes ¶
RoundDeltaMinutes 计算两个时间之间的分钟数差异,并四舍五入
func SmallerFirst ¶
SmallerFirst 将两个时间按照从小到大的顺序排列
func SmallerLast ¶
SmallerLast 将两个时间按照从大到小的顺序排列
func StandardExponentialBackoff ¶ added in v0.6.0
func StandardExponentialBackoff(count, maxRetries int, baseDelay, maxDelay time.Duration) time.Duration
StandardExponentialBackoff 退避指数函数用于计算下一次重试将在多长时间后发生,当返回 -1 时表示不再重试
- count:当前重试次数
- maxRetries:最大重试次数,当为负数时表示无限重试
该函数将以 2 为基数,0.5 为随机化因子进行计算
func ToDuration ¶
ToDuration 将一个数值转换为 time.Duration 类型,当 unit 为空时,默认为纳秒单位
func ToDurationDay ¶
func ToDurationDay[V constraints.Number](v V) time.Duration
ToDurationDay 将一个数值转换为天的 time.Duration 类型
func ToDurationHour ¶
func ToDurationHour[V constraints.Number](v V) time.Duration
ToDurationHour 将一个数值转换为小时的 time.Duration 类型
func ToDurationMinute ¶
func ToDurationMinute[V constraints.Number](v V) time.Duration
ToDurationMinute 将一个数值转换为分钟的 time.Duration 类型
func ToDurationSecond ¶
func ToDurationSecond[V constraints.Number](v V) time.Duration
ToDurationSecond 将一个数值转换为秒的 time.Duration 类型
func ToDurationWeek ¶
func ToDurationWeek[V constraints.Number](v V) time.Duration
ToDurationWeek 将一个数值转换为周的 time.Duration 类型
func UnregisterTask ¶
func UnregisterTask(name string)
UnregisterTask 调用内置时间调度器 BuiltInScheduler 的 Scheduler.UnregisterTask 函数
Types ¶
type Adjuster ¶
type Adjuster struct {
// contains filtered or unexported fields
}
Adjuster 时间调节器是一个用于获取偏移时间的工具
func BuiltInAdjuster ¶
func BuiltInAdjuster() *Adjuster
BuiltInAdjuster 获取内置的由 NewAdjuster(0) 函数创建的时间调节器
type Period ¶
Period 表示一个时间段
func NewPeriodWindow ¶
NewPeriodWindow 创建一个特定长度的时间窗口
func NewPeriodWindowWeek ¶
NewPeriodWindowWeek 创建一周长度的时间窗口,从周一零点开始至周日 23:59:59 结束
func NewPeriodWithDay ¶
NewPeriodWithDay 创建一个时间段,从 t 开始,持续 day 天
func NewPeriodWithDayZero ¶
NewPeriodWithDayZero 创建一个时间段,从 t 开始,持续到 day 天后的 0 点
func NewPeriodWithHour ¶
NewPeriodWithHour 创建一个时间段,从 t 开始,持续 hour 小时
func NewPeriodWithMicrosecond ¶
NewPeriodWithMicrosecond 创建一个时间段,从 t 开始,持续 microsecond 微秒
func NewPeriodWithMillisecond ¶
NewPeriodWithMillisecond 创建一个时间段,从 t 开始,持续 millisecond 毫秒
func NewPeriodWithMinute ¶
NewPeriodWithMinute 创建一个时间段,从 t 开始,持续 minute 分钟
func NewPeriodWithNanosecond ¶
NewPeriodWithNanosecond 创建一个时间段,从 t 开始,持续 nanosecond 纳秒
func NewPeriodWithSecond ¶
NewPeriodWithSecond 创建一个时间段,从 t 开始,持续 second 秒
func NewPeriodWithTimeArray ¶
NewPeriodWithTimeArray 创建一个时间段
func (Period) IsBetweenOrEqual ¶
IsBetweenOrEqual 判断指定时间是否在时间段之间或者等于时间段的开始或结束时间
func (Period) IsBetweenOrEqualPeriod ¶
IsBetweenOrEqualPeriod 判断指定时间是否在时间段之间或者等于时间段的开始或结束时间
type Scheduler ¶
type Scheduler struct {
// contains filtered or unexported fields
}
Scheduler 并发安全的时间调度器
func BuiltInScheduler ¶
func BuiltInScheduler() *Scheduler
BuiltInScheduler 获取内置的由 NewScheduler(DefaultSchedulerTick, BuiltInSchedulerWheelSize) 创建的时间调度器
func NewDefaultScheduler ¶
func NewDefaultScheduler() *Scheduler
NewDefaultScheduler 创建一个默认的时间调度器
- tick: DefaultSchedulerTick
- wheelSize: DefaultSchedulerWheelSize
func (*Scheduler) GetRegisteredTasks ¶
GetRegisteredTasks 获取所有未执行完成的任务名称
func (*Scheduler) RegisterAfterTask ¶
RegisterAfterTask 注册一个在特定时间后执行一次的任务
func (*Scheduler) RegisterDayMomentTask ¶
func (s *Scheduler) RegisterDayMomentTask(name string, lastExecuted time.Time, offset time.Duration, hour, min, sec int, function any, args ...any)
RegisterDayMomentTask 注册一个在每天特定时刻执行的任务
- 其中 lastExecuted 为上次执行时间,adjust 为时间偏移量,hour、min、sec 为时、分、秒
- 当上次执行时间被错过时,将会立即执行一次
func (*Scheduler) RegisterImmediateCronTask ¶
func (s *Scheduler) RegisterImmediateCronTask(name, expression string, function any, args ...any) error
RegisterImmediateCronTask 与 RegisterCronE 相同,但是会立即执行一次
func (*Scheduler) RegisterRepeatedTask ¶
func (s *Scheduler) RegisterRepeatedTask(name string, after, interval time.Duration, times int, function any, args ...any)
RegisterRepeatedTask 注册一个在特定时间后反复执行的任务
func (*Scheduler) SetExecutor ¶
func (s *Scheduler) SetExecutor(executor SchedulerExecutor)
SetExecutor 设置任务执行器
type SchedulerExecutor ¶ added in v0.6.0
type SchedulerExecutor func(name string, caller func(), separate func())
type StateLine ¶
type StateLine[State comparable] struct { // contains filtered or unexported fields }
StateLine 表示一个状态时间线,它记录了一系列时间点及其对应的状态和触发器。 在时间线中,每个时间点都与一个状态和一组触发器相关联,可以通过时间点查找状态,并触发与之相关联的触发器。
func NewStateLine ¶
func NewStateLine[State comparable](zero State) *StateLine[State]
NewStateLine 创建一个从左向右由早到晚的状态时间线
func (*StateLine[State]) AddState ¶
func (s *StateLine[State]) AddState(state State, t time.Time, onTrigger ...func()) *StateLine[State]
AddState 添加一个状态到时间线中,状态不能与任一时间点重合,否则将被忽略
- onTrigger: 该状态绑定的触发器,该触发器不会被主动执行,需要主动获取触发器执行
func (*StateLine[State]) AddTriggerToState ¶
AddTriggerToState 给指定状态添加触发器
func (*StateLine[State]) Check ¶
Check 根据状态顺序检查时间线是否合法
- missingAllowed: 是否允许状态缺失,如果为 true,则状态可以不连续,如果为 false,则状态必须连续
状态不连续表示时间线中存在状态缺失,例如:
- 状态为 [1, 2, 3, 4, 5] 的时间线,如果 missingAllowed 为 true,则状态为 [1, 3, 5] 也是合法的
- 状态为 [1, 2, 3, 4, 5] 的时间线,如果 missingAllowed 为 false,则状态为 [1, 3, 5] 是不合法的
func (*StateLine[State]) GetIndexByState ¶
GetIndexByState 获取指定状态的索引
func (*StateLine[State]) GetLastState ¶
func (s *StateLine[State]) GetLastState() State
GetLastState 获取最后一个状态
func (*StateLine[State]) GetMissingStates ¶
func (s *StateLine[State]) GetMissingStates(states ...State) []State
GetMissingStates 获取缺失的状态
func (*StateLine[State]) GetNextStateTimeByIndex ¶
GetNextStateTimeByIndex 获取指定索引的下一个时间点
func (*StateLine[State]) GetNextTimeByState ¶
GetNextTimeByState 获取指定状态的下一个时间点
func (*StateLine[State]) GetPrevStateTimeByIndex ¶
GetPrevStateTimeByIndex 获取指定索引的上一个时间点
func (*StateLine[State]) GetPrevTimeByState ¶
GetPrevTimeByState 获取指定状态的上一个时间点
func (*StateLine[State]) GetStateByIndex ¶
GetStateByIndex 获取指定索引的状态
func (*StateLine[State]) GetStateByTime ¶
GetStateByTime 获取指定时间点的状态
func (*StateLine[State]) GetStateCount ¶
GetStateCount 获取状态数量
func (*StateLine[State]) GetStateIndexByTime ¶
GetStateIndexByTime 获取指定时间点的索引
func (*StateLine[State]) GetTimeByIndex ¶
GetTimeByIndex 获取指定索引的时间点
func (*StateLine[State]) GetTimeByState ¶
GetTimeByState 获取指定状态的时间点
func (*StateLine[State]) GetTriggerByIndex ¶
GetTriggerByIndex 获取指定索引的触发器
func (*StateLine[State]) GetTriggerByState ¶
func (s *StateLine[State]) GetTriggerByState(state State) []func()
GetTriggerByState 获取指定状态的触发器
func (*StateLine[State]) GetTriggerByTime ¶
GetTriggerByTime 获取指定时间点的触发器
func (*StateLine[State]) IterateReverse ¶
IterateReverse 按照时间逆序遍历时间线