changes as per #5
This commit is contained in:
parent
4f0ef12848
commit
5442b2bfef
12
README.md
12
README.md
|
@ -77,30 +77,30 @@ Import the library:
|
||||||
|
|
||||||
Simplest way:
|
Simplest way:
|
||||||
|
|
||||||
nextTime := cronexpr.Parse("0 0 29 2 *").Next(time.Now())
|
nextTime := cronexpr.MustParse("0 0 29 2 *").Next(time.Now())
|
||||||
|
|
||||||
Assuming `time.Now()` is "2013-08-29 09:28:00", then `nextTime` will be "2016-02-29 00:00:00".
|
Assuming `time.Now()` is "2013-08-29 09:28:00", then `nextTime` will be "2016-02-29 00:00:00".
|
||||||
|
|
||||||
You can keep the returned Expression pointer around if you want to reuse it:
|
You can keep the returned Expression pointer around if you want to reuse it:
|
||||||
|
|
||||||
expr := cronexpr.Parse("0 0 29 2 *")
|
expr := cronexpr.MustParse("0 0 29 2 *")
|
||||||
nextTime := expr.Next(time.Now())
|
nextTime := expr.Next(time.Now())
|
||||||
...
|
...
|
||||||
nextTime = expr.Next(nextTime)
|
nextTime = expr.Next(nextTime)
|
||||||
|
|
||||||
Use `time.IsZero()` to find out whether a valid time was returned. For example,
|
Use `time.IsZero()` to find out whether a valid time was returned. For example,
|
||||||
|
|
||||||
cronexpr.Parse("* * * * * 1980").Next(time.Now()).IsZero()
|
cronexpr.MustParse("* * * * * 1980").Next(time.Now()).IsZero()
|
||||||
|
|
||||||
will return `true`, whereas
|
will return `true`, whereas
|
||||||
|
|
||||||
cronexpr.Parse("* * * * * 2050").Next(time.Now()).IsZero()
|
cronexpr.MustParse("* * * * * 2050").Next(time.Now()).IsZero()
|
||||||
|
|
||||||
will return `false` (as of 2013-08-29...)
|
will return `false` (as of 2013-08-29...)
|
||||||
|
|
||||||
You may also query for `n` next time stamps:
|
You may also query for `n` next time stamps:
|
||||||
|
|
||||||
cronexpr.Parse("0 0 29 2 *").NextN(time.Now(), 5)
|
cronexpr.MustParse("0 0 29 2 *").NextN(time.Now(), 5)
|
||||||
|
|
||||||
which returns a slice of time.Time objects, containing the following time stamps (as of 2013-08-30):
|
which returns a slice of time.Time objects, containing the following time stamps (as of 2013-08-30):
|
||||||
|
|
||||||
|
@ -111,7 +111,7 @@ which returns a slice of time.Time objects, containing the following time stamps
|
||||||
2032-02-29 00:00:00
|
2032-02-29 00:00:00
|
||||||
|
|
||||||
The time zone of time values returned by `Next` and `NextN` is always the
|
The time zone of time values returned by `Next` and `NextN` is always the
|
||||||
time zone of the time value passed as argument, unless zero time value is
|
time zone of the time value passed as argument, unless a zero time value is
|
||||||
returned.
|
returned.
|
||||||
|
|
||||||
API
|
API
|
||||||
|
|
13
cronexpr.go
13
cronexpr.go
|
@ -45,11 +45,12 @@ type Expression struct {
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
// Parse returns a new Expression pointer. It expects a well-formed cron expression.
|
// MustParse returns a new Expression pointer. It expects a well-formed cron
|
||||||
// If a malformed cron expression is supplied, the result is undefined. See
|
// expression. If a malformed cron expression is supplied, it will `panic`.
|
||||||
// <https://github.com/gorhill/cronexpr#implementation> for documentation
|
// See <https://github.com/gorhill/cronexpr#implementation> for documentation
|
||||||
// about what is a well-formed cron expression from this library point of view.
|
// about what is a well-formed cron expression from this library's point of
|
||||||
func Parse(cronLine string) *Expression {
|
// view.
|
||||||
|
func MustParse(cronLine string) *Expression {
|
||||||
cronLineNormalized := cronNormalize(cronLine)
|
cronLineNormalized := cronNormalize(cronLine)
|
||||||
|
|
||||||
// Split into fields
|
// Split into fields
|
||||||
|
@ -73,7 +74,7 @@ func Parse(cronLine string) *Expression {
|
||||||
// At this point, we should have at least 7 fields. Fields beyond the
|
// At this point, we should have at least 7 fields. Fields beyond the
|
||||||
// seventh one, if any, are ignored.
|
// seventh one, if any, are ignored.
|
||||||
if len(cronFields) < 7 {
|
if len(cronFields) < 7 {
|
||||||
panic("Malformed cron expression\n")
|
panic("MustParse(): Not enough fields in the cron time expression\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generic parser can be used for most fields
|
// Generic parser can be used for most fields
|
||||||
|
|
|
@ -13,10 +13,10 @@ package cronexpr_test
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gorhill/cronexpr"
|
|
||||||
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorhill/cronexpr"
|
||||||
)
|
)
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
@ -123,7 +123,7 @@ func TestExpressions(t *testing.T) {
|
||||||
for _, test := range crontests {
|
for _, test := range crontests {
|
||||||
for _, times := range test.times {
|
for _, times := range test.times {
|
||||||
from, _ := time.Parse("2006-01-02 15:04:05", times.from)
|
from, _ := time.Parse("2006-01-02 15:04:05", times.from)
|
||||||
next := cronexpr.Parse(test.expr).Next(from)
|
next := cronexpr.MustParse(test.expr).Next(from)
|
||||||
nextstr := next.Format(test.layout)
|
nextstr := next.Format(test.layout)
|
||||||
if nextstr != times.next {
|
if nextstr != times.next {
|
||||||
t.Errorf(`("%s").Next("%s") = "%s", got "%s"`, test.expr, times.from, times.next, nextstr)
|
t.Errorf(`("%s").Next("%s") = "%s", got "%s"`, test.expr, times.from, times.next, nextstr)
|
||||||
|
@ -134,18 +134,41 @@ func TestExpressions(t *testing.T) {
|
||||||
|
|
||||||
func TestZero(t *testing.T) {
|
func TestZero(t *testing.T) {
|
||||||
from, _ := time.Parse("2006-01-02", "2013-08-31")
|
from, _ := time.Parse("2006-01-02", "2013-08-31")
|
||||||
next := cronexpr.Parse("* * * * * 1980").Next(from)
|
next := cronexpr.MustParse("* * * * * 1980").Next(from)
|
||||||
if next.IsZero() == false {
|
if next.IsZero() == false {
|
||||||
t.Error(`("* * * * * 1980").Next("2013-08-31").IsZero() returned 'false', expected 'true'`)
|
t.Error(`("* * * * * 1980").Next("2013-08-31").IsZero() returned 'false', expected 'true'`)
|
||||||
}
|
}
|
||||||
|
|
||||||
next = cronexpr.Parse("* * * * * 2050").Next(from)
|
next = cronexpr.MustParse("* * * * * 2050").Next(from)
|
||||||
if next.IsZero() == true {
|
if next.IsZero() == true {
|
||||||
t.Error(`("* * * * * 2050").Next("2013-08-31").IsZero() returned 'true', expected 'false'`)
|
t.Error(`("* * * * * 2050").Next("2013-08-31").IsZero() returned 'true', expected 'false'`)
|
||||||
}
|
}
|
||||||
|
|
||||||
next = cronexpr.Parse("* * * * * 2099").Next(time.Time{})
|
next = cronexpr.MustParse("* * * * * 2099").Next(time.Time{})
|
||||||
if next.IsZero() == false {
|
if next.IsZero() == false {
|
||||||
t.Error(`("* * * * * 2014").Next(time.Time{}).IsZero() returned 'true', expected 'false'`)
|
t.Error(`("* * * * * 2014").Next(time.Time{}).IsZero() returned 'true', expected 'false'`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNextN(t *testing.T) {
|
||||||
|
expected := []string{
|
||||||
|
"Sat, 30 Nov 2013 00:00:00",
|
||||||
|
"Sat, 29 Mar 2014 00:00:00",
|
||||||
|
"Sat, 31 May 2014 00:00:00",
|
||||||
|
"Sat, 30 Aug 2014 00:00:00",
|
||||||
|
"Sat, 29 Nov 2014 00:00:00",
|
||||||
|
}
|
||||||
|
from, _ := time.Parse("2006-01-02 15:04:05", "2013-09-02 08:44:30")
|
||||||
|
result := cronexpr.MustParse("0 0 * * 6#5").NextN(from, uint(len(expected)))
|
||||||
|
if len(result) != len(expected) {
|
||||||
|
t.Errorf(`MustParse("0 0 * * 6#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:15")
|
||||||
|
if nextStr != expected[i] {
|
||||||
|
t.Errorf(`MustParse("0 0 * * 6#5").NextN("2013-09-02 08:44:30", 5):\n"`)
|
||||||
|
t.Errorf(` result[%d]: expected "%s" but got "%s"`, i, expected[i], nextStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ import (
|
||||||
// ExampleParse
|
// ExampleParse
|
||||||
func ExampleParse() {
|
func ExampleParse() {
|
||||||
t := time.Date(2013, time.August, 31, 0, 0, 0, 0, time.UTC)
|
t := time.Date(2013, time.August, 31, 0, 0, 0, 0, time.UTC)
|
||||||
nextTimes := cronexpr.Parse("0 0 29 2 *").NextN(t, 5)
|
nextTimes := cronexpr.MustParse("0 0 29 2 *").NextN(t, 5)
|
||||||
for i := range nextTimes {
|
for i := range nextTimes {
|
||||||
fmt.Println(nextTimes[i].Format(time.RFC1123))
|
fmt.Println(nextTimes[i].Format(time.RFC1123))
|
||||||
// Output:
|
// Output:
|
||||||
|
|
Loading…
Reference in New Issue