Skip to content

Commit 00c940b

Browse files
author
noah
committed
Add README.md
1 parent 2f4b9b0 commit 00c940b

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Cron expression parser
2+
3+
**Given a cron expression, you can get the previous timestamp or the next timestamp that satisfies the cron expression.**
4+
5+
I have used cron expression syntax to implement a new feature called *deploy freeze window* in Gitploy.
6+
7+
## Install
8+
9+
```shell
10+
go get github.com/gitploy-io/cronexpr
11+
```
12+
13+
## Usage
14+
15+
Import the package, first:
16+
17+
```go
18+
import "time"
19+
import "github.com/gitploy-io/cronexpr"
20+
```
21+
22+
```go
23+
prevTime := cronexpr.MustParse("0 0 29 * *").Prev(time.Now())
24+
nextTime := cronexpr.MustParse("0 0 29 * *").Next(time.Now())
25+
```
26+
27+
You can check the detail in the `_example` directory.
28+
29+
30+
## Implementation
31+
32+
33+
```
34+
Field name Mandatory? Allowed values Allowed special characters
35+
---------- ---------- -------------- --------------------------
36+
Minutes Yes 0-59 * / , -
37+
Hours Yes 0-23 * / , -
38+
Day of month Yes 1-31 * / , -
39+
Month Yes 1-12 or JAN-DEC * / , -
40+
Day of week Yes 0-6 or SUN-SAT * / , -
41+
```
42+
43+
### Asterisk ( * )
44+
The asterisk indicates that the cron expression matches for all values of the field. E.g., using an asterisk in the 4th field (month) indicates every month.
45+
46+
### Slash ( / )
47+
Slashes describe increments of ranges. For example `3-59/15` in the minute field indicate the third minute of the hour and every 15 minutes thereafter. The form `*/...` is equivalent to the form "first-last/...", that is, an increment over the largest possible range of the field.
48+
49+
### Comma ( , )
50+
Commas are used to separate items of a list. For example, using `MON,WED,FRI` in the 5th field (day of week) means Mondays, Wednesdays and Fridays.
51+
52+
### Hyphen ( - )
53+
Hyphens define ranges. For example, 2000-2010 indicates every year between 2000 and 2010 AD, inclusive.
54+
File renamed without changes.

_example/simple.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/gitploy-io/cronexpr"
8+
)
9+
10+
func main() {
11+
t := time.Date(2013, 8, 29, 9, 28, 0, 0, time.UTC)
12+
13+
nextTime := cronexpr.MustParse("0 0 29 2 *").Next(t)
14+
fmt.Print(nextTime)
15+
}

0 commit comments

Comments
 (0)