From 1116fee3fc45d9c5f5826dbfe90a4d915fc3fafa Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Tue, 12 Apr 2022 21:16:09 +0200 Subject: [PATCH] Actually possibly kinda decent cron next func --- src/cron/cron.v | 6 ----- src/cron/expression.v | 6 ++--- src/cron/expression_test.v | 53 +++++++++++++++----------------------- 3 files changed, 24 insertions(+), 41 deletions(-) diff --git a/src/cron/cron.v b/src/cron/cron.v index 931d1c8d..25263269 100644 --- a/src/cron/cron.v +++ b/src/cron/cron.v @@ -1,9 +1,7 @@ module cron import git -import datatypes import time -import rand struct ScheduledBuild { repo git.GitRepo @@ -15,12 +13,8 @@ fn (r1 ScheduledBuild) < (r2 ScheduledBuild) bool { } pub fn cron(conf Config) ? { - mut queue := datatypes.MinHeap{} - ce := parse_expression('0 3') ? t := time.parse('2002-01-01 00:00:00') ? - - println(t) t2 := ce.next(t) ? println(t2) } diff --git a/src/cron/expression.v b/src/cron/expression.v index 600e252b..0a355418 100644 --- a/src/cron/expression.v +++ b/src/cron/expression.v @@ -38,17 +38,17 @@ pub fn (ce &CronExpression) next(ref time.Time) ?time.Time { month_index++ } - if month_index < ce.months.len { + if month_index < ce.months.len && sref.month == ce.months[month_index] { for day_index < ce.days.len && sref.day > ce.days[day_index] { day_index++ } - if day_index < ce.days.len { + if day_index < ce.days.len && ce.days[day_index] == sref.day { for hour_index < ce.hours.len && sref.hour > ce.hours[hour_index] { hour_index++ } - if hour_index < ce.hours.len { + if hour_index < ce.hours.len && ce.hours[hour_index] == sref.hour { // Minute is the only value where we explicitely make sure we // can't match sref's value exactly. This is to ensure we only // return values in the future. diff --git a/src/cron/expression_test.v b/src/cron/expression_test.v index aaead9c4..ce8526b6 100644 --- a/src/cron/expression_test.v +++ b/src/cron/expression_test.v @@ -2,38 +2,27 @@ module cron import time { parse } +fn util_test_time(exp string, t1_str string, t2_str string) ? { + ce := parse_expression(exp) ? + t1 := parse(t1_str) ? + t2 := parse(t2_str) ? + + t3 := ce.next(t1) ? + + assert t2.year == t3.year + assert t2.month == t3.month + assert t2.day == t3.day + assert t2.hour == t3.hour + assert t2.minute == t3.minute +} + fn test_next_simple() ? { - ce := parse_expression('0 3') ? - t := parse('2002-01-01 00:00:00') ? - t2 := ce.next(t) ? + // Very simple + util_test_time('0 3', '2002-01-01 00:00:00', '2002-01-01 03:00:00') ? - assert t2.year == 2002 - assert t2.month == 1 - assert t2.day == 1 - assert t2.hour == 3 - assert t2.minute == 0 -} - -fn test_next_identical() ? { - ce := parse_expression('0 3') ? - t := parse('2002-01-01 03:00:00') ? - t2 := ce.next(t) ? - - assert t2.year == 2002 - assert t2.month == 1 - assert t2.day == 2 - assert t2.hour == 3 - assert t2.minute == 0 -} - -fn test_next_next_day() ? { - ce := parse_expression('0 3') ? - t := parse('2002-01-01 04:00:00') ? - t2 := ce.next(t) ? - - assert t2.year == 2002 - assert t2.month == 1 - assert t2.day == 2 - assert t2.hour == 3 - assert t2.minute == 0 + // Overlap to next day + util_test_time('0 3', '2002-01-01 03:00:00', '2002-01-02 03:00:00') ? + util_test_time('0 3', '2002-01-01 04:00:00', '2002-01-02 03:00:00') ? + + util_test_time('0 3/4', '2002-01-01 04:00:00', '2002-01-01 07:00:00') ? }