Browse Source

Merge pull request #2 from tilinna/master

Some comment and test fixes
Rob Figueiredo 12 years ago
parent
commit
004a5153dd
2 changed files with 15 additions and 12 deletions
  1. 3 8
      parser.go
  2. 12 4
      spec_test.go

+ 3 - 8
parser.go

@@ -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, "*")
 	}
@@ -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 {

+ 12 - 4
spec_test.go

@@ -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 ?", ""},
@@ -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)
 		}
 	}
@@ -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)
+			}
 		}
 	}