This Go application parses and validates cron job time fields using a set of rules and generates execution intervals based on the parsed values.
The application parses cron expressions for minute, hour, day of month, month, and day of week fields. It handles wildcards (*), ranges (1-15), and lists (1,10,15) using modular parser functions.
Does NOT support special time strings, such as @yearly
- Parsing Wildcards (*, */15, */30)
- Parsing Ranges (1-15)
- Parsing Lists (1,10,15)
- Go (version 1.17 or later)
- Unzipping the given .zip file
go run cronparser.go '*/15 0 1,15 * 1-5 /usr/bin/find'
Replace */15 0 1,15 * 1-5 /usr/bin/find
with your cron expression.
An already built executable has been provided
go build cronparser.go
./cronparser '*/15 0 1,15 * 1-5 /usr/bin/find'
Example Output
$ ./cronparser '*/15 0 1,15 * 1-5 /usr/bin/find'
minute 0 15 30 45
hour 0
day of month 1 15
month 1 2 3 4 5 6 7 8 9 10 11 12
day of week 1 2 3 4 5
command /usr/bin/find
The application uses a set of rules to determine how to parse different formats of cron time fields:
- Wildcards Rule: Parses expressions like *, */15, */30.
- Ranges Rule: Parses expressions like 1-15.
- List Rule: Parses expressions like 1,10,15.
Upon successful parsing of each cron time field, the application outputs the parsed intervals:
minute 0 15 30 45
hour 0
day of month 1 15
month 1 2 3 4 5 6 7 8 9 10 11 12
day of week 1 2 3 4 5
command /usr/bin/find
Tests can be ran with the following:
go test ./...
Since being a commandline tool, i feel benchmarking is not necessary
The application provides error handling for incorrect cron time field formats and values outside expected ranges.
This process should be simple. Within the parser package, you should see the ruleset defined.
For example:
// Rules provides functions for parsing different formats of the cronjob time formats
var Rules = []parseRule{
// parse wildcards '*' '*/15' '*/30'
{
Comparitor: func(field string) bool {
return strings.Contains(field, "*")
},
Parse: parseWildcards,
},
// parse ranges '1-15'
{
Comparitor: func(field string) bool {
return strings.Contains(field, "-")
},
Parse: parseRanges,
},
// parse list '1,10,15'
{
Comparitor: func(field string) bool {
return strings.Contains(field, ",")
},
Parse: parseList,
},
}
- The
Comparitor
function controls when the rule should be ran - The
Parse
function does the actual parsing. And returns a slice of time intervals.