Documentation
¶
Overview ¶
* @Author: kamalyes 501893067@qq.com * @Date: 2025-12-24 19:30:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-12-25 11:59:31 * @FilePath: \go-toolbox\pkg\cron\expression.go * @Description: Cron 表达式字段解析器 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
Index ¶
- Constants
- Variables
- func GetExpression(alias string) (string, bool)
- func ParseExprToBits[T types.Numerical](expr string, bounds types.Bounds[T], starBit uint64) (uint64, error)
- func ParseFieldToBits[T types.Numerical](field string, bounds types.Bounds[T], starBit uint64) (uint64, error)
- func ParseFieldWithSpecialChars(fieldStr string, bounds cronBounds, starBit uint64, isDOM bool, isDOW bool) (bits uint64, lastDay bool, lastWeekday bool, nearestWeekday int, lastDow int, ...)
- func ParseIntOrName[T types.Numerical](expr string, names map[string]T) (T, error)
- type CronEverySchedule
- type CronParseOption
- type CronParser
- type CronSchedule
- type CronSpecSchedule
- func (s *CronSpecSchedule) Next(t time.Time) time.Time
- func (s *CronSpecSchedule) WithDom(dom uint64) *CronSpecSchedule
- func (s *CronSpecSchedule) WithDow(dow uint64) *CronSpecSchedule
- func (s *CronSpecSchedule) WithHour(hour uint64) *CronSpecSchedule
- func (s *CronSpecSchedule) WithMinute(minute uint64) *CronSpecSchedule
- func (s *CronSpecSchedule) WithMonth(month uint64) *CronSpecSchedule
- func (s *CronSpecSchedule) WithSecond(second uint64) *CronSpecSchedule
Constants ¶
const ( EverySecond = "* * * * * *" // 每秒 EveryMinute = "0 * * * * *" // 每分钟 Every5Minutes = "0 */5 * * * *" // 每5分钟 Every15Minutes = "0 */15 * * * *" // 每15分钟 Every30Minutes = "0 */30 * * * *" // 每30分钟 EveryHour = "0 0 * * * *" // 每小时 Every2Hours = "0 0 */2 * * *" // 每2小时 Every3Hours = "0 0 */3 * * *" // 每3小时 Every6Hours = "0 0 */6 * * *" // 每6小时 Every12Hours = "0 0 */12 * * *" // 每12小时 EveryDay = "0 0 0 * * *" // 每天午夜 EveryWeek = "0 0 0 * * 0" // 每周日午夜 EveryMonth = "0 0 0 1 * *" // 每月1号午夜 EveryYear = "0 0 0 1 1 *" // 每年1月1日午夜 )
基础周期表达式(6字段:秒 分 时 日 月 周)
const ( WorkdaysOnly = "0 0 0 * * 1-5" // 工作日(周一到周五)午夜 WeekendsOnly = "0 0 0 * * 0,6" // 周末(周六和周日)午夜 WorkdaysAt9AM = "0 0 9 * * 1-5" // 工作日早9点 WorkdaysAt6PM = "0 0 18 * * 1-5" // 工作日晚6点 WeekendAt10AM = "0 0 10 * * 0,6" // 周末早10点 )
工作日与周末表达式
const ( Monday = "0 0 0 * * 1" // 每周一午夜 Tuesday = "0 0 0 * * 2" // 每周二午夜 Wednesday = "0 0 0 * * 3" // 每周三午夜 Thursday = "0 0 0 * * 4" // 每周四午夜 Friday = "0 0 0 * * 5" // 每周五午夜 Saturday = "0 0 0 * * 6" // 每周六午夜 Sunday = "0 0 0 * * 0" // 每周日午夜 )
具体星期表达式
const ( DailyAt9AM = "0 0 9 * * *" // 每天早9点 DailyAt12PM = "0 0 12 * * *" // 每天中午12点 DailyAt6PM = "0 0 18 * * *" // 每天晚6点 DailyAt11PM = "0 0 23 * * *" // 每天晚11点 MorningHours = "0 0 6-12 * * *" // 早晨时段(6-12点) AfternoonHours = "0 0 12-18 * * *" // 下午时段(12-18点) EveningHours = "0 0 18-23 * * *" // 晚上时段(18-23点) BusinessHours = "0 0 9-17 * * *" // 营业时间(9-17点) PeakHours = "0 0 8-9,17-18 * * *" // 高峰期(8-9点和17-18点) OffPeakHours = "0 0 0-6,22-23 * * *" // 低谷期(0-6点和22-23点) )
特定时段表达式
const ( FirstDayOfMonth = "0 0 0 1 * *" // 每月第1天午夜 LastDayOfMonth = "0 0 0 L * *" // 每月最后一天午夜(需要特殊处理) MidMonth = "0 0 0 15 * *" // 每月15号午夜 MonthlyReport = "0 0 2 1 * *" // 每月1号凌晨2点(报表生成) )
月度表达式
const ( QuarterlyReport = "0 0 2 1 */3 *" // 每季度首日凌晨2点 YearlyReport = "0 0 2 1 1 *" // 每年1月1日凌晨2点 )
季度和年度表达式
const ( Every10Seconds = "*/10 * * * * *" // 每10秒(测试用) Every30Seconds = "*/30 * * * * *" // 每30秒(测试用) )
调试和测试表达式
Variables ¶
var CronSecondParser = NewCronParser( CronSecond | CronMinute | CronHour | CronDom | CronMonth | CronDow | CronDescriptor, )
CronSecondParser 带秒的解析器(秒 分 时 日 月 周,6个字段)
var CronStandardParser = NewCronParser( CronMinute | CronHour | CronDom | CronMonth | CronDow | CronDescriptor, )
CronStandardParser 标准解析器(分 时 日 月 周,5个字段)
var ExpressionAliases = map[string]string{ "@secondly": EverySecond, "@minutely": EveryMinute, "@hourly": EveryHour, "@daily": EveryDay, "@midnight": EveryDay, "@weekly": EveryWeek, "@monthly": EveryMonth, "@yearly": EveryYear, "@annually": EveryYear, "@workdays": WorkdaysOnly, "@weekends": WeekendsOnly, "@every_5min": Every5Minutes, "@every_15min": Every15Minutes, "@every_30min": Every30Minutes, "@every_2h": Every2Hours, "@every_3h": Every3Hours, "@every_6h": Every6Hours, "@every_12h": Every12Hours, "@monday": Monday, "@tuesday": Tuesday, "@wednesday": Wednesday, "@thursday": Thursday, "@friday": Friday, "@saturday": Saturday, "@sunday": Sunday, "@daily_9am": DailyAt9AM, "@daily_12pm": DailyAt12PM, "@daily_6pm": DailyAt6PM, "@daily_11pm": DailyAt11PM, "@business_hours": BusinessHours, "@morning_hours": MorningHours, "@afternoon": AfternoonHours, "@evening": EveningHours, "@peak_hours": PeakHours, "@off_peak": OffPeakHours, "@workdays_9am": WorkdaysAt9AM, "@workdays_6pm": WorkdaysAt6PM, "@weekend_10am": WeekendAt10AM, "@first_of_month": FirstDayOfMonth, "@mid_month": MidMonth, "@monthly_report": MonthlyReport, "@quarterly": QuarterlyReport, "@yearly_report": YearlyReport, "@every_10s": Every10Seconds, "@every_30s": Every30Seconds, }
ExpressionAliases 表达式别名映射
Functions ¶
func ParseExprToBits ¶
func ParseExprToBits[T types.Numerical](expr string, bounds types.Bounds[T], starBit uint64) (uint64, error)
ParseExprToBits 解析单个 Cron 表达式,支持: number | start-end[/step] | * | ? 示例: "1-10/2", "jan-mar", "*/5", "*" 注意:不处理 L, W, #, C 等特殊字符,这些由 ParseFieldWithSpecialChars 处理
func ParseFieldToBits ¶
func ParseFieldToBits[T types.Numerical](field string, bounds types.Bounds[T], starBit uint64) (uint64, error)
ParseFieldToBits 解析逗号分隔的字段表达式,返回位集合 示例: "1-5,10,15-20/2" -> bits starBit: 当表达式为纯 * 或 ? 时,设置此位(用于 Dom/Dow 特殊逻辑)
func ParseFieldWithSpecialChars ¶
func ParseFieldWithSpecialChars(fieldStr string, bounds cronBounds, starBit uint64, isDOM bool, isDOW bool) ( bits uint64, lastDay bool, lastWeekday bool, nearestWeekday int, lastDow int, nthDow int, err error, )
ParseFieldWithSpecialChars 解析可能包含特殊字符的字段 返回值:bits, lastDay, lastWeekday, nearestWeekday, lastDow, nthDow, error
Types ¶
type CronEverySchedule ¶
CronEverySchedule 间隔调度(@every duration)
type CronParseOption ¶
type CronParseOption int
CronParseOption 解析选项配置
const ( // CronSecond 秒字段,默认 0 CronSecond CronParseOption = 1 << iota // CronSecondOptional 可选秒字段,默认 0 CronSecondOptional // CronMinute 分钟字段,默认 0 CronMinute // CronHour 小时字段,默认 0 CronHour // CronDom 月中日期字段,默认 * CronDom // CronMonth 月份字段,默认 * CronMonth // CronDow 星期字段,默认 * CronDow // CronDowOptional 可选星期字段,默认 * CronDowOptional // CronDescriptor 允许描述符,如 @monthly, @weekly 等 CronDescriptor )
type CronParser ¶
type CronParser struct {
// contains filtered or unexported fields
}
CronParser Cron 表达式解析器
func NewCronParser ¶
func NewCronParser(options CronParseOption) *CronParser
NewCronParser 创建一个自定义配置的解析器
示例:
标准解析器(不包含秒)
parser := NewCronParser(CronMinute | CronHour | CronDom | CronMonth | CronDow)
schedule, err := parser.Parse("0 0 15 */3 *")
包含秒的解析器
parser := NewCronParser(CronSecond | CronMinute | CronHour | CronDom | CronMonth | CronDow)
schedule, err := parser.Parse("0 0 0 15 */3 *")
func (*CronParser) Parse ¶
func (p *CronParser) Parse(spec string) (CronSchedule, error)
Parse 解析 cron 表达式并返回调度规范 支持时区、描述符和标准 cron 表达式
type CronSchedule ¶
CronSchedule Cron 调度接口
func ParseCronStandard ¶
func ParseCronStandard(spec string) (CronSchedule, error)
ParseCronStandard 使用标准解析器解析 cron 表达式(5个字段)
func ParseCronWithSeconds ¶
func ParseCronWithSeconds(spec string) (CronSchedule, error)
ParseCronWithSeconds 使用带秒的解析器解析 cron 表达式(6个字段)
type CronSpecSchedule ¶
type CronSpecSchedule struct {
// 各字段的位掩码
Second, Minute, Hour, Dom, Month, Dow uint64
Location *time.Location
// 特殊字符标记
LastDay bool // L - 月份最后一天
LastWeekday bool // LW - 月份最后一个工作日
NearestWeekday int // W - 最近的工作日(如 15W,存储15)
LastDow int // 星期字段的 L(如 6L,存储6表示最后一个星期五)
NthDow int // 第几个星期X的星期(如 6#3,高位存储6,低位存储3)
}
CronSpecSchedule Cron 规范调度(基于位集合存储,高性能)
func NewZeroCronSpecSchedule ¶
func NewZeroCronSpecSchedule(loc *time.Location) *CronSpecSchedule
NewZeroCronSpecSchedule 创建一个基础的 CronSpecSchedule(所有字段设置为0时刻和通配符)
func (*CronSpecSchedule) Next ¶
func (s *CronSpecSchedule) Next(t time.Time) time.Time
Next 计算下次执行时间(支持纳秒精度)
func (*CronSpecSchedule) WithDom ¶
func (s *CronSpecSchedule) WithDom(dom uint64) *CronSpecSchedule
WithDom 设置月中日期字段
func (*CronSpecSchedule) WithDow ¶
func (s *CronSpecSchedule) WithDow(dow uint64) *CronSpecSchedule
WithDow 设置星期字段
func (*CronSpecSchedule) WithHour ¶
func (s *CronSpecSchedule) WithHour(hour uint64) *CronSpecSchedule
WithHour 设置小时字段
func (*CronSpecSchedule) WithMinute ¶
func (s *CronSpecSchedule) WithMinute(minute uint64) *CronSpecSchedule
WithMinute 设置分钟字段
func (*CronSpecSchedule) WithMonth ¶
func (s *CronSpecSchedule) WithMonth(month uint64) *CronSpecSchedule
WithMonth 设置月份字段
func (*CronSpecSchedule) WithSecond ¶
func (s *CronSpecSchedule) WithSecond(second uint64) *CronSpecSchedule
WithSecond 设置秒字段