Failed attempt at x,y,z cron stuff [CI SKIP]

main
Jef Roosens 2022-04-11 22:52:06 +02:00
parent 0e5f31e649
commit ab4f64b6b6
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 28 additions and 15 deletions

View File

@ -48,7 +48,7 @@ pub fn cron(conf Config) ? {
// mut t := time.now() // mut t := time.now()
println(t) println(t)
for _ in 1..25 { for _ in 1 .. 25 {
t = ce.next(t) ? t = ce.next(t) ?
println(t) println(t)
} }

View File

@ -78,11 +78,11 @@ pub fn (ce &CronExpression) next(ref time.Time) ?time.Time {
// month, e.g. day 30 in February. When this occurs, we reset day back to // month, e.g. day 30 in February. When this occurs, we reset day back to
// the smallest value & loop over to the next month that does have this // the smallest value & loop over to the next month that does have this
// day. // day.
if day > days_in_month[ce.months[month_index % ce.months.len] - 1] { if day > cron.days_in_month[ce.months[month_index % ce.months.len] - 1] {
day = ce.days[0] day = ce.days[0]
month_index += 1 month_index += 1
for day > days_in_month[ce.months[month_index & ce.months.len] - 1] { for day > cron.days_in_month[ce.months[month_index & ce.months.len] - 1] {
month_index += 1 month_index += 1
// If for whatever reason the day value ends up being something // If for whatever reason the day value ends up being something
@ -94,7 +94,6 @@ pub fn (ce &CronExpression) next(ref time.Time) ?time.Time {
} }
} }
month := ce.months[month_index % ce.months.len] month := ce.months[month_index % ce.months.len]
mut year := ref.year mut year := ref.year
@ -118,8 +117,7 @@ fn (ce &CronExpression) next_from_now() ?time.Time {
// parse_range parses a given string into a range of sorted integers, if // parse_range parses a given string into a range of sorted integers, if
// possible. // possible.
fn parse_range(s string, min int, max int) ?[]int { fn parse_range(s string, min int, max int, mut bitv []bool) ? {
mut out := []int{}
mut start := min mut start := min
mut interval := 1 mut interval := 1
@ -134,18 +132,35 @@ fn parse_range(s string, min int, max int) ?[]int {
// Here, s solely consists of a number, so that's the only value we // Here, s solely consists of a number, so that's the only value we
// should return. // should return.
else { else {
return [start] bitv[start - min - 1] = true
return
} }
} }
if interval == 0 { if interval == 0 {
return [] return
} }
for start <= max { for start <= max {
out << start bitv[start - min - 1] = true
start += interval start += interval
} }
}
fn parse_part(s string, min int, max int) ?[]int {
mut bitv := []bool{init: false, len: max - min + 1}
for range in s.split(',') {
parse_range(range, min, max, mut bitv) ?
}
mut out := []int{}
for i in 0..max + 1 {
if bitv[i] {
out << min + i
}
}
return out return out
} }
@ -165,9 +180,9 @@ fn parse_expression(exp string) ?CronExpression {
} }
return CronExpression{ return CronExpression{
minutes: parse_range(parts[0], 0, 59) ? minutes: parse_part(parts[0], 0, 59) ?
hours: parse_range(parts[1], 0, 23) ? hours: parse_part(parts[1], 0, 23) ?
days: parse_range(parts[2], 1, 31) ? days: parse_part(parts[2], 1, 31) ?
months: parse_range(parts[3], 1, 12) ? months: parse_part(parts[3], 1, 12) ?
} }
} }

View File

@ -40,5 +40,3 @@ fn test_parse_step_number_too_large() ? {
fn test_parse_step_number_too_small() ? { fn test_parse_step_number_too_small() ? {
assert parse_range('2/4', 5, 10) ? == [5, 9] assert parse_range('2/4', 5, 10) ? == [5, 9]
} }