Merge fcf95785ff into 88b0669f7d
				
					
				
			This commit is contained in:
		
						commit
						423255f4bf
					
				
							
								
								
									
										26
									
								
								cronexpr.go
								
								
								
								
							
							
						
						
									
										26
									
								
								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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -305,6 +305,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{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue