-
Notifications
You must be signed in to change notification settings - Fork 4
/
cronexpr.go
95 lines (90 loc) · 2.1 KB
/
cronexpr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*!
* Copyright 2015 Jan Guth
*
* Project: github.com/fentas/cronexpr
* File: cronexpr.go
* Version: 0.1.0
* License: AGPLv3 see <https://www.gnu.org/licenses/agpl-3.0.html>
*
*/
package main
import (
"os"
"github.com/codegangsta/cli"
"github.com/gorhill/cronexpr"
"time"
"fmt"
"strconv"
"github.com/hhkbp2/go-strftime"
//"github.com/jehiah/go-strftime"
//"github.com/tebeka/strftime"
)
func main() {
app := cli.NewApp()
app.Version = "0.1.0"
app.Name = "cronexpr"
app.Usage = "convert cron expression and get next occurance"
app.Flags = []cli.Flag {
cli.StringFlag{
Name: "unix, u",
Value: "",
Usage: "from specific unix timestamp",
},
cli.StringFlag{
Name: "format, f",
Value: "",
Usage: "format options see http://strftime.org/",
},
cli.StringFlag{
Name: "next, n",
Value: "",
Usage: "n next time stamps",
},
cli.StringFlag{
Name: "utc",
Value: "false",
Usage: "n next time stamps",
},
}
app.Action = func(c *cli.Context) {
cron := ""
if len(c.Args()) > 0 {
cron = c.Args()[0]
} else {
panic("missing cron expression")
}
from := time.Now()
if c.String("unix") != "" {
u, err := strconv.ParseInt(c.String("unix"), 10, 64)
if err != nil {
panic(err)
}
from = time.Unix(u, 0)
}
if c.BoolT("utc") {
from = from.UTC()
}
if c.String("next") != "" {
n, err := strconv.ParseInt(c.String("next"), 10, 64)
if err != nil {
panic(err)
}
result := cronexpr.MustParse(cron).NextN(from, uint(n))
for _, next := range result {
out := strconv.FormatInt(next.Unix(), 10)
if c.String("format") != "" {
out = strftime.Format(c.String("format"), next)
}
fmt.Println(out)
}
} else {
result := cronexpr.MustParse(cron).Next(from)
out := strconv.FormatInt(result.Unix(), 10)
if c.String("format") != "" {
out = strftime.Format(c.String("format"), result)
}
fmt.Println(out)
}
}
app.Run(os.Args)
}