Merge pull request #7 from safx/fix-parser

fixed parsering bug for like "*/5"
This commit is contained in:
Raymond Hill 2014-04-23 19:11:08 -04:00
commit 815886d9f2
2 changed files with 50 additions and 11 deletions

View File

@ -411,6 +411,17 @@ func genericFieldParse(s string, desc fieldDescriptor) ([]*cronDirective, error)
send: indices[i][1], send: indices[i][1],
} }
snormal := strings.ToLower(s[indices[i][0]:indices[i][1]]) snormal := strings.ToLower(s[indices[i][0]:indices[i][1]])
// `*/2`
pairs := makeLayoutRegexp(layoutWildcardAndInterval, desc.valuePattern).FindStringSubmatchIndex(snormal)
if len(pairs) > 0 {
directive.kind = span
directive.first = desc.min
directive.last = desc.max
directive.step = atoi(snormal[pairs[2]:pairs[3]])
directives = append(directives, &directive)
continue
}
// `*` // `*`
if makeLayoutRegexp(layoutWildcard, desc.valuePattern).MatchString(snormal) { if makeLayoutRegexp(layoutWildcard, desc.valuePattern).MatchString(snormal) {
directive.kind = all directive.kind = all
@ -428,7 +439,7 @@ func genericFieldParse(s string, desc fieldDescriptor) ([]*cronDirective, error)
continue continue
} }
// `5-20` // `5-20`
pairs := makeLayoutRegexp(layoutRange, desc.valuePattern).FindStringSubmatchIndex(snormal) pairs = makeLayoutRegexp(layoutRange, desc.valuePattern).FindStringSubmatchIndex(snormal)
if len(pairs) > 0 { if len(pairs) > 0 {
directive.kind = span directive.kind = span
directive.first = desc.atoi(snormal[pairs[2]:pairs[3]]) directive.first = desc.atoi(snormal[pairs[2]:pairs[3]])
@ -437,16 +448,6 @@ func genericFieldParse(s string, desc fieldDescriptor) ([]*cronDirective, error)
directives = append(directives, &directive) directives = append(directives, &directive)
continue continue
} }
// `*/2`
pairs = makeLayoutRegexp(layoutWildcardAndInterval, desc.valuePattern).FindStringSubmatchIndex(snormal)
if len(pairs) > 0 {
directive.kind = span
directive.first = desc.min
directive.last = desc.max
directive.step = atoi(snormal[pairs[2]:pairs[3]])
directives = append(directives, &directive)
continue
}
// `5/2` // `5/2`
pairs = makeLayoutRegexp(layoutValueAndInterval, desc.valuePattern).FindStringSubmatchIndex(snormal) pairs = makeLayoutRegexp(layoutValueAndInterval, desc.valuePattern).FindStringSubmatchIndex(snormal)
if len(pairs) > 0 { if len(pairs) > 0 {

View File

@ -50,6 +50,21 @@ var crontests = []crontest{
}, },
}, },
// every 5 Second
{
"*/5 * * * * * *",
"2006-01-02 15:04:05",
[]crontimes{
{"2013-01-01 00:00:00", "2013-01-01 00:00:05"},
{"2013-01-01 00:00:59", "2013-01-01 00:01:00"},
{"2013-01-01 00:59:59", "2013-01-01 01:00:00"},
{"2013-01-01 23:59:59", "2013-01-02 00:00:00"},
{"2013-02-28 23:59:59", "2013-03-01 00:00:00"},
{"2016-02-28 23:59:59", "2016-02-29 00:00:00"},
{"2012-12-31 23:59:59", "2013-01-01 00:00:00"},
},
},
// Minutes // Minutes
{ {
"* * * * *", "* * * * *",
@ -246,6 +261,29 @@ func TestNextN(t *testing.T) {
} }
} }
func TestNextN_every5min(t *testing.T) {
expected := []string{
"Mon, 2 Sep 2013 08:45:00",
"Mon, 2 Sep 2013 08:50:00",
"Mon, 2 Sep 2013 08:55:00",
"Mon, 2 Sep 2013 09:00:00",
"Mon, 2 Sep 2013 09:05:00",
}
from, _ := time.Parse("2006-01-02 15:04:05", "2013-09-02 08:44:32")
result := cronexpr.MustParse("*/5 * * * *").NextN(from, uint(len(expected)))
if len(result) != len(expected) {
t.Errorf(`MustParse("*/5 * * * *").NextN("2013-09-02 08:44:30", 5):\n"`)
t.Errorf(` Expected %d returned time values but got %d instead`, len(expected), len(result))
}
for i, next := range result {
nextStr := next.Format("Mon, 2 Jan 2006 15:04:05")
if nextStr != expected[i] {
t.Errorf(`MustParse("*/5 * * * *").NextN("2013-09-02 08:44:30", 5):\n"`)
t.Errorf(` result[%d]: expected "%s" but got "%s"`, i, expected[i], nextStr)
}
}
}
/******************************************************************************/ /******************************************************************************/
var benchmarkExpressions = []string{ var benchmarkExpressions = []string{