forked from vieter-v/vieter
Can't figure out cron algo right now [CI SKIP]
parent
799fe2e454
commit
e3da3d0d7f
|
@ -1,6 +1,7 @@
|
|||
module cron
|
||||
|
||||
import math
|
||||
import time
|
||||
|
||||
struct CronExpression {
|
||||
minutes []u32
|
||||
|
@ -8,7 +9,32 @@ struct CronExpression {
|
|||
days []u32
|
||||
}
|
||||
|
||||
// parse_range parses a given string into a range of integers, if possible.
|
||||
// next calculates the earliest time this cron expression is valid.
|
||||
pub fn (ce &CronExpression) next(ref &time.Time) time.Time {
|
||||
res := time.Time{}
|
||||
|
||||
mut day := 0
|
||||
mut hour := 0
|
||||
mut minute := 0
|
||||
|
||||
// Find the next minute
|
||||
// If ref.minute is greater than
|
||||
if ref.minute >= ce.minutes[ce.minutes.len - 1] || ref.minute < ce.minutes[0] {
|
||||
minute = ce.minutes[0]
|
||||
}else{
|
||||
for i in 0..ce.minutes.len {
|
||||
if ce.minutes[i] > ref.minute {
|
||||
minute = ce.minutes[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// parse_range parses a given string into a range of sorted integers, if
|
||||
// possible.
|
||||
fn parse_range(s string, min u32, max u32) ?[]u32 {
|
||||
mut out := []u32{}
|
||||
mut start := min
|
||||
|
@ -29,6 +55,10 @@ fn parse_range(s string, min u32, max u32) ?[]u32 {
|
|||
}
|
||||
}
|
||||
|
||||
if interval == 0 {
|
||||
return []
|
||||
}
|
||||
|
||||
for start <= max {
|
||||
out << start
|
||||
start += interval
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
module cron
|
||||
|
||||
// =====parse_range=====
|
||||
fn test_parse_star_range() ? {
|
||||
assert parse_range('*', 0, 5) ? == [u32(0), 1, 2, 3, 4, 5]
|
||||
}
|
||||
|
@ -24,6 +25,10 @@ fn test_parse_step_star_too_large() ? {
|
|||
assert parse_range('*/21', 0, 20) ? == [u32(0)]
|
||||
}
|
||||
|
||||
fn test_parse_step_zero() ? {
|
||||
assert parse_range('*/0', 0, 20) ? == []
|
||||
}
|
||||
|
||||
fn test_parse_step_number() ? {
|
||||
assert parse_range('5/4', 0, 20) ? == [u32(5), 9, 13, 17]
|
||||
}
|
||||
|
@ -35,3 +40,5 @@ fn test_parse_step_number_too_large() ? {
|
|||
fn test_parse_step_number_too_small() ? {
|
||||
assert parse_range('2/4', 5, 10) ? == [u32(5), 9]
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue