From e3da3d0d7f1ad0f1d55c2d9c7e775ef3f2670ff6 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sun, 10 Apr 2022 17:47:46 +0200 Subject: [PATCH] Can't figure out cron algo right now [CI SKIP] --- src/cron/expression.v | 32 +++++++++++++++++++++++++++++++- src/cron/expression_test.v | 7 +++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/cron/expression.v b/src/cron/expression.v index 87819005..a29bf536 100644 --- a/src/cron/expression.v +++ b/src/cron/expression.v @@ -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 diff --git a/src/cron/expression_test.v b/src/cron/expression_test.v index 6d293c56..abd5e5ff 100644 --- a/src/cron/expression_test.v +++ b/src/cron/expression_test.v @@ -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] } + +