Alpha version cron 'next' function

This commit is contained in:
Jef Roosens 2022-04-11 22:16:31 +02:00
parent e3da3d0d7f
commit 135b6c3d7f
Signed by untrusted user: Jef Roosens
GPG key ID: B75D4F293C7052DB
3 changed files with 135 additions and 38 deletions

View file

@ -3,50 +3,117 @@ module cron
import math
import time
const days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
struct CronExpression {
minutes []u32
hours []u32
days []u32
minutes []int
hours []int
days []int
months []int
}
// next calculates the earliest time this cron expression is valid.
pub fn (ce &CronExpression) next(ref &time.Time) time.Time {
res := time.Time{}
pub fn (ce &CronExpression) next(ref time.Time) ?time.Time {
mut minute_index := 0
mut hour_index := 0
mut day_index := 0
mut month_index := 0
mut day := 0
mut hour := 0
mut minute := 0
for month_index < ce.months.len && ref.month > ce.months[month_index] {
month_index++
}
// 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
if month_index < ce.months.len {
for day_index < ce.days.len && ref.day > ce.days[day_index] {
day_index++
}
if day_index < ce.days.len {
for hour_index < ce.hours.len && ref.hour > ce.hours[hour_index] {
hour_index++
}
if hour_index < ce.hours.len {
// For each unit, we calculate what the next value is
for minute_index < ce.minutes.len && ref.minute >= ce.minutes[minute_index] {
minute_index++
}
}
}
}
return res
// Sometime we have to shift values one more
if minute_index == ce.minutes.len && hour_index < ce.hours.len {
hour_index += 1
}
if hour_index == ce.hours.len && day_index < ce.days.len {
day_index += 1
}
if day_index == ce.days.len && month_index < ce.months.len {
month_index += 1
}
mut minute := ce.minutes[minute_index % ce.minutes.len]
mut hour := ce.hours[hour_index % ce.hours.len]
mut day := ce.days[day_index % ce.days.len]
mut reset := false
// If the day can't be planned in the current month, we go to the next one
// and go back to day one
if day > days_in_month[ce.months[month_index % ce.months.len] - 1] {
month_index += 1
day = ce.days[0]
// Make sure we only plan in a month that the day occurs in
for day > days_in_month[ce.months[month_index & ce.months.len] - 1] {
month_index += 1
// Prevent scenario where there are no months that can be scheduled.
if month_index == 2 * ce.months.len {
return error('No schedulable moment.')
}
}
}
month := ce.months[month_index % ce.months.len]
mut year := ref.year
if month_index >= ce.months.len {
year++
}
return time.Time{
year: year
month: month
day: day
minute: minute
hour: hour
}
}
fn (ce &CronExpression) next_from_now() ?time.Time {
return ce.next(time.now())
}
// 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{}
fn parse_range(s string, min int, max int) ?[]int {
mut out := []int{}
mut start := min
mut interval := u32(1)
mut interval := 1
if s != '*' {
exps := s.split('/')
start = math.min(max, math.max(exps[0].u32(), min))
start = math.min(max, math.max(exps[0].int(), min))
if exps.len > 1 {
interval = exps[1].u32()
interval = exps[1].int()
}
// Here, s solely consists of a number, so that's the only value we
// should return.
@ -69,15 +136,22 @@ fn parse_range(s string, min u32, max u32) ?[]u32 {
// min hour day month day-of-week
fn parse_expression(exp string) ?CronExpression {
parts := exp.split(' ')
mut parts := exp.split(' ')
if parts.len != 3 {
return error('Expression must contain 5 space-separated parts.')
if parts.len < 2 || parts.len > 4 {
return error('Expression must contain between 2 and 4 space-separated parts.')
}
// For ease of use, we allow the user to only specify as many parts as they
// need.
for parts.len < 4 {
parts << '*'
}
return CronExpression{
minutes: parse_range(parts[0], 0, 59) ?
hours: parse_range(parts[1], 0, 23) ?
days: parse_range(parts[2], 0, 31) ?
days: parse_range(parts[2], 1, 31) ?
months: parse_range(parts[3], 1, 12) ?
}
}