From fcf95785ff885e54d5e9f361bd584084890e05cb Mon Sep 17 00:00:00 2001 From: Carl Fooks Date: Mon, 5 Mar 2018 14:23:11 +0000 Subject: [PATCH] Add Match function --- cronexpr.go | 26 +++++++++++++++++++++++++- cronexpr_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/cronexpr.go b/cronexpr.go index 58b518f..40b7843 100644 --- a/cronexpr.go +++ b/cronexpr.go @@ -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 @@ -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 +} diff --git a/cronexpr_test.go b/cronexpr_test.go index f170769..8e8f2ca 100644 --- a/cronexpr_test.go +++ b/cronexpr_test.go @@ -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{