no need for NoMatch(), existing time.IsZero() does the job well

This commit is contained in:
gorhill 2013-08-30 15:10:15 -04:00
parent d3a85f92d8
commit ae9c090812
2 changed files with 7 additions and 28 deletions

View File

@ -88,13 +88,13 @@ to create a `CronExpression` object once and keep a copy of it for reuse:
nextTime := cronexpr.NextTime(time.Now())
...
Use `cronexpression.NoMatch()` to find out whether a valid time was returned. For example,
Use `nextTime.IsZero()` to find out whether a valid time was returned. For example,
cronexpression.NoMatch(cronexpression.NextTime("* * * * * 1980", time.Now()))
cronexpression.NextTime("* * * * * 1980", time.Now()).IsZero()
will return `true`, whereas
cronexpression.NoMatch(cronexpression.NextTime("* * * * * 2050", time.Now()))
cronexpression.NextTime("* * * * * 2050", time.Now()).IsZero()
will return `false` (as of 2013-08-29...)
@ -143,9 +143,3 @@ Given a time stamp `fromTime`, return the closest following time stamp which mat
Given a time stamp `fromTime`, return a slice of `n` closest following time stamps which match the cron expression `cronexpr`. The time stamps in the returned slice are in chronological ascending order. The `time.Location` of the returned time stamps is the same as `fromTime`.
#### func NoMatch
func NoMatch(t time.Time) bool
Returns `true` if time stamp `t` is not a valid time stamp from `CronExpression` point of view. An invalid time stamp is returned by this library whenever no matching time stamp is found given a specific cron expression.

View File

@ -45,10 +45,6 @@ type CronExpression struct {
/******************************************************************************/
var noMatchTime = time.Date(2100, 7, 1, 0, 0, 0, 0, time.UTC)
/******************************************************************************/
// NewCronExpression() returns a new CronExpression pointer. It expects
// a well-formed cron expression. If a malformed cron expression is
// supplied, the result is undefined.
@ -127,7 +123,7 @@ func NextTimeN(cronLine string, fromTime time.Time, n int) []time.Time {
// time stamp is the same as `fromTime`.
func (cronexpr *CronExpression) NextTime(fromTime time.Time) time.Time {
// Special case
if NoMatch(fromTime) {
if fromTime.IsZero() {
return fromTime
}
@ -145,7 +141,7 @@ func (cronexpr *CronExpression) NextTime(fromTime time.Time) time.Time {
v := fromTime.Year()
i := sort.SearchInts(cronexpr.yearList, v)
if i == len(cronexpr.yearList) {
return noMatchTime
return time.Time{}
}
if v != cronexpr.yearList[i] {
return cronexpr.nextYear(fromTime)
@ -218,7 +214,7 @@ func (cronexpr *CronExpression) NextTimeN(fromTime time.Time, n int) []time.Time
nextTimes := make([]time.Time, 0)
fromTime = cronexpr.NextTime(fromTime)
for {
if NoMatch(fromTime) {
if fromTime.IsZero() {
break
}
nextTimes = append(nextTimes, fromTime)
@ -233,23 +229,12 @@ func (cronexpr *CronExpression) NextTimeN(fromTime time.Time, n int) []time.Time
/******************************************************************************/
// Returns `true` if time stamp `t` is not a valid time stamp from
// `CronExpression` point of view. An invalid time stamp is returned by this
// library whenever no matching time stamp is found given a specific cron
// expression.
func NoMatch(t time.Time) bool {
// https://en.wikipedia.org/wiki/Cron#CRON_expression: 19702099
return t.Year() >= 2100
}
/******************************************************************************/
func (cronexpr *CronExpression) nextYear(t time.Time) time.Time {
// Find index at which item in list is greater or equal to
// candidate year
i := sort.SearchInts(cronexpr.yearList, t.Year()+1)
if i == len(cronexpr.yearList) {
return noMatchTime
return time.Time{}
}
// Year changed, need to recalculate actual days of month
cronexpr.actualDaysOfMonthList = cronexpr.calculateActualDaysOfMonth(cronexpr.yearList[i], cronexpr.monthList[0])