Skip to content

Commit

Permalink
feat:support ? dow && dom
Browse files Browse the repository at this point in the history
  • Loading branch information
alanoluo committed Aug 10, 2023
1 parent 42e4dec commit 187fcf4
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions cronexpr_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ var (
/******************************************************************************/

var (
layoutWildcard = `^\*$|^\?$`
layoutWildcard = `^\*$`
layoutDayWeek = `^\?$`
layoutValue = `^(%value%)$`
layoutRange = `^(%value%)-(%value%)$`
layoutWildcardAndInterval = `^\*/(\d+)$`
Expand Down Expand Up @@ -267,7 +268,7 @@ type cronDirective struct {
}

func genericFieldHandler(s string, desc fieldDescriptor) ([]int, error) {
directives, err := genericFieldParse(s, desc)
directives, err := genericFieldParse(s, desc, false)
if err != nil {
return nil, err
}
Expand All @@ -293,7 +294,7 @@ func (expr *Expression) dowFieldHandler(s string) error {
expr.lastWeekDaysOfWeek = make(map[int]bool)
expr.specificWeekDaysOfWeek = make(map[int]bool)

directives, err := genericFieldParse(s, dowDescriptor)
directives, err := genericFieldParse(s, dowDescriptor, true)
if err != nil {
return err
}
Expand All @@ -309,7 +310,7 @@ func (expr *Expression) dowFieldHandler(s string) error {
populateOne(expr.lastWeekDaysOfWeek, dowDescriptor.atoi(snormal[pairs[2]:pairs[3]]))
} else {
// `5#3`
pairs := makeLayoutRegexp(layoutDowOfSpecificWeek, dowDescriptor.valuePattern).FindStringSubmatchIndex(snormal)
pairs = makeLayoutRegexp(layoutDowOfSpecificWeek, dowDescriptor.valuePattern).FindStringSubmatchIndex(snormal)
if len(pairs) > 0 {
populateOne(expr.specificWeekDaysOfWeek, (dowDescriptor.atoi(snormal[pairs[4]:pairs[5]])-1)*7+(dowDescriptor.atoi(snormal[pairs[2]:pairs[3]])%7))
} else {
Expand All @@ -335,7 +336,7 @@ func (expr *Expression) domFieldHandler(s string) error {
expr.daysOfMonth = make(map[int]bool) // days of month map
expr.workdaysOfMonth = make(map[int]bool) // work days of month map

directives, err := genericFieldParse(s, domDescriptor)
directives, err := genericFieldParse(s, domDescriptor, true)
if err != nil {
return err
}
Expand Down Expand Up @@ -399,7 +400,7 @@ func toList(set map[int]bool) []int {

/******************************************************************************/

func genericFieldParse(s string, desc fieldDescriptor) ([]*cronDirective, error) {
func genericFieldParse(s string, desc fieldDescriptor, questionMark bool) ([]*cronDirective, error) {
// At least one entry must be present
indices := entryFinder.FindAllStringIndex(s, -1)
if len(indices) == 0 {
Expand All @@ -424,6 +425,15 @@ func genericFieldParse(s string, desc fieldDescriptor) ([]*cronDirective, error)
directives = append(directives, &directive)
continue
}
// `?`
if questionMark && makeLayoutRegexp(layoutDayWeek, desc.valuePattern).MatchString(snormal) {
directive.kind = all
directive.first = desc.min
directive.last = desc.max
directive.step = 1
directives = append(directives, &directive)
continue
}
// `5`
if makeLayoutRegexp(layoutValue, desc.valuePattern).MatchString(snormal) {
directive.kind = one
Expand Down

0 comments on commit 187fcf4

Please sign in to comment.