算出cron表達式接下來幾次執行時間


1.使用cron庫

  1. 需要使用的go庫:【點擊跳轉】。
  2. 具體使用方法可以參照例子使用,下面主要實現計算接下來幾次cron表達式執行時間。
package main

import (
	"flag"
	"fmt"
	"log"
	"time"

	"github.com/robfig/cron"
)

func main() {
	spec := flag.String("s", "* * * * * *", "cron spec")
	next := flag.Uint("i", 5, "cron next times")
	flag.Parse()

	p, err := cron.Parse(*spec)
	if err != nil {
		log.Fatal(err)
	}
	t := time.Now()
	for i := int(*next); i > 0; i-- {
		t = p.Next(t)
		fmt.Println(t.String())
	}
}

下面是執行結果:
cron.exe -s "20 10 1 * * *" -i 2
2019-09-11 01:10:20 +0800 CST
2019-09-12 01:10:20 +0800 CST

2.總結

    工作中很多時候需要驗證自己寫的cron表達式是否正確,可以用上面的方法來測試。當然網上有很多在線網站也是可以做到的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM