Skip to content
This repository was archived by the owner on Jan 11, 2019. It is now read-only.

Add Matches function #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion cronexpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func Parse(cronLine string) (*Expression, error) {
fieldCount = 7
}

var expr = Expression{}
var expr = Expression{expression:cronLine}
var field = 0
var err error

Expand Down Expand Up @@ -264,3 +264,27 @@ func (expr *Expression) NextN(fromTime time.Time, n uint) []time.Time {
}
return nextTimes
}

func (expr *Expression) Matches(refTime time.Time) bool {
testSecondsAndYears := expr.testSeconds()
return (!testSecondsAndYears || containsVal(expr.secondList, refTime.Second())) &&
containsVal(expr.minuteList, refTime.Minute()) &&
containsVal(expr.hourList, refTime.Hour()) &&
expr.daysOfMonth[refTime.Day()] &&
containsVal(expr.monthList, int(refTime.Month())) &&
expr.daysOfWeek[int(refTime.Weekday())] &&
(!testSecondsAndYears || containsVal(expr.yearList, refTime.Year()))
}

func containsVal(sl []int, v int) bool {
for _, i := range sl {
if i == v {
return true
}
}
return false
}

func (expr *Expression) testSeconds() bool {
return len(fieldFinder.FindAllStringIndex(expr.expression, -1)) > 6
}
38 changes: 38 additions & 0 deletions cronexpr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,44 @@ func TestInterval_Interval60Issue(t *testing.T){
}
}

func TestMatches(t *testing.T) {
var cases = []struct {
cronLine string
refTime string
shouldMatch bool
}{
// m h dom mon dow
{"5 * * * *", "2013-09-02 01:44:32", false}, // minutes
{"5 * * * *", "2013-09-02 01:05:32", true},
{"* 3 * * *", "2013-09-02 01:45:32", false}, // hours
{"* 3 * * *", "2013-09-02 03:45:32", true},
{"* * 2 * *", "2013-09-03 03:45:32", false}, // day of month
{"* * 2 * *", "2013-09-02 03:45:32", true},
{"* * * 8 *", "2013-09-03 03:45:32", false}, // month
{"* * * 9 *", "2013-09-03 03:45:32", true},
{"* * * * 5", "2018-03-01 03:45:32", false}, // day of week
{"* * * * 4", "2018-03-01 03:45:32", true},

// s m h dom mon dow year
{"0 * * * * * *", "2018-03-01 03:45:32", false}, // seconds
{"32 * * * * * *", "2018-03-01 03:45:32", true},
{"* * * * * * 2013", "2018-03-01 03:45:32", false}, // years
{"* * * * * * 2018", "2018-03-01 03:45:32", true},

{"32 */15 1/2 1 3 4 2018", "2018-03-01 03:45:32", true},
}

for i, c := range cases {
expression, _ := cronexpr.Parse(c.cronLine)
refTime, _ := time.Parse("2006-01-02 15:04:05", c.refTime)
matches := expression.Matches(refTime)
if matches != c.shouldMatch {
t.Errorf("Case %d: Parse(%q).Matches(T{%q}) returned '%t', expected '%t'", i+1, c.cronLine, c.refTime, matches, c.shouldMatch)
}
}
}


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

var benchmarkExpressions = []string{
Expand Down