diff --git a/cronexpr_parse.go b/cronexpr_parse.go index 2a0e5bc..a1ab438 100644 --- a/cronexpr_parse.go +++ b/cronexpr_parse.go @@ -411,6 +411,17 @@ func genericFieldParse(s string, desc fieldDescriptor) ([]*cronDirective, error) send: 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) { directive.kind = all @@ -428,7 +439,7 @@ func genericFieldParse(s string, desc fieldDescriptor) ([]*cronDirective, error) continue } // `5-20` - pairs := makeLayoutRegexp(layoutRange, desc.valuePattern).FindStringSubmatchIndex(snormal) + pairs = makeLayoutRegexp(layoutRange, desc.valuePattern).FindStringSubmatchIndex(snormal) if len(pairs) > 0 { directive.kind = span 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) 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` pairs = makeLayoutRegexp(layoutValueAndInterval, desc.valuePattern).FindStringSubmatchIndex(snormal) if len(pairs) > 0 { diff --git a/cronexpr_test.go b/cronexpr_test.go index 20c75c3..b729170 100644 --- a/cronexpr_test.go +++ b/cronexpr_test.go @@ -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 { "* * * * *", @@ -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{