瀏覽代碼

Recommend CRON_TZ=(Location) to specify the time zone of a schedule.

This is done to comply with https://en.wikipedia.org/wiki/Cron

The previous TZ=(Location) behavior will be maintained.
Rob Figueiredo 6 年之前
父節點
當前提交
4813ba2496
共有 5 個文件被更改,包括 23 次插入10 次删除
  1. 4 0
      README.md
  2. 6 4
      doc.go
  3. 4 3
      parser.go
  4. 3 3
      parser_test.go
  5. 6 0
      spec_test.go

+ 4 - 0
README.md

@@ -34,6 +34,10 @@ Updates required:
   UPDATING: Code that sets Cron.ErrorLogger or calls Cron.SetLocation must be
   updated to provide those values on construction.
 
+- CRON_TZ is now the recommended way to specify the timezone of a single
+  schedule, which is sanctioned by the specification. The legacy "TZ=" prefix
+  will continue to be supported since it is unambiguous and easy to do so.
+
 ### Background - Cron spec format
 
 There are two cron spec formats in common usage:

+ 6 - 4
doc.go

@@ -8,7 +8,7 @@ them in their own goroutines.
 
 	c := cron.New()
 	c.AddFunc("0 30 * * * *", func() { fmt.Println("Every hour on the half hour") })
-	c.AddFunc("TZ=Asia/Tokyo 30 04 * * * *", func() { fmt.Println("Runs at 04:30 Tokyo time every day") })
+	c.AddFunc("CRON_TZ=Asia/Tokyo 30 04 * * * *", func() { fmt.Println("Runs at 04:30 Tokyo time every day") })
 	c.AddFunc("@hourly",      func() { fmt.Println("Every hour") })
 	c.AddFunc("@every 1h30m", func() { fmt.Println("Every hour thirty") })
 	c.Start()
@@ -124,7 +124,7 @@ time zone (time.Local). You can specify a different time zone on construction:
 
 Individual cron schedules may also override the time zone they are to be
 interpreted in by providing an additional space-separated field at the beginning
-of the cron spec, of the form "TZ=Asia/Tokyo".
+of the cron spec, of the form "CRON_TZ=Asia/Tokyo".
 
 For example:
 
@@ -137,12 +137,14 @@ For example:
 	c.AddFunc("0 6 * * ?", ...)
 
 	# Runs at 6am in Asia/Tokyo
-	cron.New().AddFunc("TZ=Asia/Tokyo 0 6 * * ?", ...)
+	cron.New().AddFunc("CRON_TZ=Asia/Tokyo 0 6 * * ?", ...)
 
 	# Runs at 6am in Asia/Tokyo
 	c := cron.New(cron.WithLocation(nyc))
 	c.SetLocation("America/New_York")
-	c.AddFunc("TZ=Asia/Tokyo 0 6 * * ?", ...)
+	c.AddFunc("CRON_TZ=Asia/Tokyo 0 6 * * ?", ...)
+
+The prefix "TZ=(TIME ZONE)" is also supported for legacy compatibility.
 
 Be aware that jobs scheduled during daylight-savings leap-ahead transitions will
 not be run!

+ 4 - 3
parser.go

@@ -92,11 +92,12 @@ func (p Parser) Parse(spec string) (Schedule, error) {
 
 	// Extract timezone if present
 	var loc = time.Local
-	if strings.HasPrefix(spec, "TZ=") {
+	if strings.HasPrefix(spec, "TZ=") || strings.HasPrefix(spec, "CRON_TZ=") {
 		var err error
 		i := strings.Index(spec, " ")
-		if loc, err = time.LoadLocation(spec[3:i]); err != nil {
-			return nil, fmt.Errorf("provided bad location %s: %v", spec[3:i], err)
+		eq := strings.Index(spec, "=")
+		if loc, err = time.LoadLocation(spec[eq+1 : i]); err != nil {
+			return nil, fmt.Errorf("provided bad location %s: %v", spec[eq+1:i], err)
 		}
 		spec = strings.TrimSpace(spec[i:])
 	}

+ 3 - 3
parser_test.go

@@ -146,9 +146,9 @@ func TestParseSchedule(t *testing.T) {
 	}{
 		{secondParser, "0 5 * * * *", every5min(time.Local)},
 		{standardParser, "5 * * * *", every5min(time.Local)},
-		{secondParser, "TZ=UTC  0 5 * * * *", every5min(time.UTC)},
-		{standardParser, "TZ=UTC  5 * * * *", every5min(time.UTC)},
-		{secondParser, "TZ=Asia/Tokyo 0 5 * * * *", every5min(tokyo)},
+		{secondParser, "CRON_TZ=UTC  0 5 * * * *", every5min(time.UTC)},
+		{standardParser, "CRON_TZ=UTC  5 * * * *", every5min(time.UTC)},
+		{secondParser, "CRON_TZ=Asia/Tokyo 0 5 * * * *", every5min(tokyo)},
 		{secondParser, "@every 5m", ConstantDelaySchedule{5 * time.Minute}},
 		{secondParser, "@midnight", midnight(time.Local)},
 		{secondParser, "TZ=UTC  @midnight", midnight(time.UTC)},

+ 6 - 0
spec_test.go

@@ -118,6 +118,12 @@ func TestNext(t *testing.T) {
 		{"2012-03-11T03:00:00-0400", "TZ=America/New_York 0 0 * * * ?", "2012-03-11T04:00:00-0400"},
 		{"2012-03-11T04:00:00-0400", "TZ=America/New_York 0 0 * * * ?", "2012-03-11T05:00:00-0400"},
 
+		// hourly job using CRON_TZ
+		{"2012-03-11T00:00:00-0500", "CRON_TZ=America/New_York 0 0 * * * ?", "2012-03-11T01:00:00-0500"},
+		{"2012-03-11T01:00:00-0500", "CRON_TZ=America/New_York 0 0 * * * ?", "2012-03-11T03:00:00-0400"},
+		{"2012-03-11T03:00:00-0400", "CRON_TZ=America/New_York 0 0 * * * ?", "2012-03-11T04:00:00-0400"},
+		{"2012-03-11T04:00:00-0400", "CRON_TZ=America/New_York 0 0 * * * ?", "2012-03-11T05:00:00-0400"},
+
 		// 1am nightly job
 		{"2012-03-11T00:00:00-0500", "TZ=America/New_York 0 0 1 * * ?", "2012-03-11T01:00:00-0500"},
 		{"2012-03-11T01:00:00-0500", "TZ=America/New_York 0 0 1 * * ?", "2012-03-12T01:00:00-0400"},