| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
package main |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
import ( |
| 16 |
"flag" |
| 17 |
"fmt" |
| 18 |
"os" |
| 19 |
"time" |
| 20 |
|
| 21 |
"github.com/gorhill/cronexpr" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
var ( |
| 27 |
usage = func() { |
| 28 |
fmt.Fprintf(os.Stderr, "usage:\n %s [options] \"{cron expression}\"\noptions:\n", os.Args[0]) |
| 29 |
flag.PrintDefaults() |
| 30 |
} |
| 31 |
inTimeStr string |
| 32 |
outTimeCount uint |
| 33 |
outTimeLayout string |
| 34 |
) |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
func main() { |
| 39 |
var err error |
| 40 |
|
| 41 |
flag.Usage = usage |
| 42 |
flag.StringVar(&inTimeStr, "t", "", `whole or partial RFC3339 time value (i.e. "2006-01-02T15:04:05Z07:00") against which the cron expression is evaluated, now if not present`) |
| 43 |
flag.UintVar(&outTimeCount, "n", 1, `number of resulting time values to output`) |
| 44 |
flag.StringVar(&outTimeLayout, "l", "Mon, 02 Jan 2006 15:04:05 MST", `Go-compliant time layout to use for outputting time value(s), see <http://golang.org/pkg/time/#pkg-constants>`) |
| 45 |
flag.Parse() |
| 46 |
|
| 47 |
cronStr := flag.Arg(0) |
| 48 |
if len(cronStr) == 0 { |
| 49 |
flag.Usage() |
| 50 |
return |
| 51 |
} |
| 52 |
|
| 53 |
inTime := time.Now() |
| 54 |
inTimeLayout := "" |
| 55 |
timeStrLen := len(inTimeStr) |
| 56 |
if timeStrLen == 2 { |
| 57 |
inTimeLayout = "06" |
| 58 |
} else if timeStrLen >= 4 { |
| 59 |
inTimeLayout += "2006" |
| 60 |
if timeStrLen >= 7 { |
| 61 |
inTimeLayout += "-01" |
| 62 |
if timeStrLen >= 10 { |
| 63 |
inTimeLayout += "-02" |
| 64 |
if timeStrLen >= 13 { |
| 65 |
inTimeLayout += "T15" |
| 66 |
if timeStrLen >= 16 { |
| 67 |
inTimeLayout += ":04" |
| 68 |
if timeStrLen >= 19 { |
| 69 |
inTimeLayout += ":05" |
| 70 |
if timeStrLen >= 20 { |
| 71 |
inTimeLayout += "Z07:00" |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if len(inTimeLayout) > 0 { |
| 81 |
|
| 82 |
if timeStrLen < 20 { |
| 83 |
inTime, err = time.ParseInLocation(inTimeLayout, inTimeStr, time.Local) |
| 84 |
} else { |
| 85 |
inTime, err = time.Parse(inTimeLayout, inTimeStr) |
| 86 |
} |
| 87 |
if err != nil { |
| 88 |
fmt.Fprintf(os.Stderr, "# error: unparseable time value: \"%s\"\n", inTimeStr) |
| 89 |
os.Exit(1) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
expr, err := cronexpr.Parse(cronStr) |
| 94 |
if err != nil { |
| 95 |
fmt.Fprintf(os.Stderr, "# %s: %s\n", os.Args[0], err) |
| 96 |
os.Exit(1) |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
fmt.Printf("# \"%s\" + \"%s\" =\n", cronStr, inTime.Format(time.RFC3339)) |
| 103 |
|
| 104 |
if outTimeCount < 1 { |
| 105 |
outTimeCount = 1 |
| 106 |
} |
| 107 |
outTimes := expr.NextN(inTime, outTimeCount) |
| 108 |
for _, outTime := range outTimes { |
| 109 |
fmt.Println(outTime.Format(outTimeLayout)) |
| 110 |
} |
| 111 |
} |