forked from vieter-v/vieter
Started writing cron expression parser [CI SKIP]
This commit is contained in:
parent
e890128bda
commit
6d60ea1538
5 changed files with 91 additions and 1 deletions
55
src/cron/expression.v
Normal file
55
src/cron/expression.v
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
module cron
|
||||
|
||||
import math
|
||||
|
||||
struct CronExpression {
|
||||
minutes []u32
|
||||
hours []u32
|
||||
days []u32
|
||||
}
|
||||
|
||||
// parse_range parses a given string into a range of integers, if possible.
|
||||
fn parse_range(s string, min u32, max u32) ?[]u32 {
|
||||
mut out := []u32{}
|
||||
mut start := min
|
||||
mut interval := u32(1)
|
||||
|
||||
if s != '*' {
|
||||
exps := s.split('/')
|
||||
|
||||
if exps.len > 1 {
|
||||
interval = exps[1].u32()
|
||||
}
|
||||
// Here, s solely consists of a number, so that's the only value we
|
||||
// should return.
|
||||
else{
|
||||
return [exps[0].u32()]
|
||||
}
|
||||
|
||||
if exps[0] != '*' {
|
||||
start = math.max(exps[0].u32(), min)
|
||||
}
|
||||
}
|
||||
|
||||
for start <= max {
|
||||
out << start
|
||||
start += interval
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
// min hour day month day-of-week
|
||||
fn parse_expression(exp string) ?CronExpression {
|
||||
parts := exp.split(' ')
|
||||
|
||||
if parts.len != 3 {
|
||||
return error("Expression must contain 5 space-separated parts.")
|
||||
}
|
||||
|
||||
return CronExpression{
|
||||
minutes: parse_range(parts[0], 0, 59) ?
|
||||
hours: parse_range(parts[1], 0, 23) ?
|
||||
days: parse_range(parts[2], 0, 31) ?
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue