Skip to content

Commit

Permalink
Merge pull request robfig#2 from tilinna/master
Browse files Browse the repository at this point in the history
Some comment and test fixes
  • Loading branch information
robfig committed Feb 26, 2013
2 parents 0b0ad46 + 96833bd commit 004a515
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
11 changes: 3 additions & 8 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ func Parse(spec string) Schedule {
return parseDescriptor(spec)
}

// Split on whitespace. We require 4 or 5 fields.
// (minute) (hour) (day of month) (month) (day of week, optional)
// Split on whitespace. We require 5 or 6 fields.
// (second) (minute) (hour) (day of month) (month) (day of week, optional)
fields := strings.Fields(spec)
if len(fields) != 5 && len(fields) != 6 {
log.Panicf("Expected 5 or 6 fields, found %d: %s", len(fields), spec)
}

// If a fifth field is not provided (DayOfWeek), then it is equivalent to star.
// If a sixth field is not provided (DayOfWeek), then it is equivalent to star.
if len(fields) == 5 {
fields = append(fields, "*")
}
Expand Down Expand Up @@ -154,11 +154,6 @@ func all(r bounds) uint64 {
return getBits(r.min, r.max, 1) | starBit
}

// first returns bits with only the first (minimum) value set.
func first(r bounds) uint64 {
return getBits(r.min, r.min, 1)
}

// parseDescriptor returns a pre-defined schedule for the expression, or panics
// if none matches.
func parseDescriptor(spec string) Schedule {
Expand Down
16 changes: 12 additions & 4 deletions spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,12 @@ func TestNext(t *testing.T) {
// Leap year
{"Mon Jul 9 23:35 2012", "0 0 0 29 Feb ?", "Mon Feb 29 00:00 2016"},

// Daylight savings time
{"Sun Mar 11 00:00 2012 EST", "0 30 2 11 Mar ?", "Mon Mar 11 02:30 2013 EDT"},
// Daylight savings time EST -> EDT
{"2012-03-11T00:00:00-0500", "0 30 2 11 Mar ?", "2013-03-11T02:30:00-0400"},

// Daylight savings time EDT -> EST
{"2012-11-04T00:00:00-0400", "0 30 2 04 Nov ?", "2012-11-04T02:30:00-0500"},
{"2012-11-04T01:45:00-0400", "0 30 1 04 Nov ?", "2012-11-04T01:30:00-0500"},

// Unsatisfiable
{"Mon Jul 9 23:35 2012", "0 0 0 30 Feb ?", ""},
Expand All @@ -115,7 +119,7 @@ func TestNext(t *testing.T) {
for _, c := range runs {
actual := Parse(c.spec).Next(getTime(c.time))
expected := getTime(c.expected)
if actual != expected {
if !actual.Equal(expected) {
t.Errorf("%s, \"%s\": (expected) %v != %v (actual)", c.time, c.spec, expected, actual)
}
}
Expand All @@ -129,10 +133,14 @@ func getTime(value string) time.Time {
if err != nil {
t, err = time.Parse("Mon Jan 2 15:04:05 2006", value)
if err != nil {
t, err = time.Parse("Mon Jan 2 15:04 2006 MST", value)
t, err = time.Parse("2006-01-02T15:04:05-0700", value)
if err != nil {
panic(err)
}
// Daylight savings time tests require location
if ny, err := time.LoadLocation("America/New_York"); err == nil {
t = t.In(ny)
}
}
}

Expand Down

0 comments on commit 004a515

Please sign in to comment.