refactor: added CronExpression.next_n function
ci/woodpecker/pr/docs Pipeline was successful Details
ci/woodpecker/pr/lint Pipeline was successful Details
ci/woodpecker/pr/build Pipeline was successful Details
ci/woodpecker/pr/docker Pipeline was successful Details
ci/woodpecker/pr/test Pipeline was successful Details

pull/201/head
Jef Roosens 2022-05-26 13:34:57 +02:00
parent bd4bb9a9fb
commit 768da5b790
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 18 additions and 8 deletions

View File

@ -2,11 +2,13 @@ module schedule
import cli
import cron.expression { parse_expression }
import time
// cmd returns the cli submodule for previewing a cron schedule.
pub fn cmd() cli.Command {
return cli.Command{
name: 'schedule'
usage: 'schedule'
description: 'Preview the behavior of a cron schedule.'
flags: [
cli.Flag{
@ -17,16 +19,10 @@ pub fn cmd() cli.Command {
},
]
execute: fn (cmd cli.Command) ? {
exp := parse_expression(cmd.args.join(' '))?
mut t := exp.next_from_now()?
println(t)
ce := parse_expression(cmd.args.join(' '))?
count := cmd.flags.get_int('count')?
for _ in 1 .. count {
t = exp.next(t)?
for t in ce.next_n(time.now(), count)? {
println(t)
}
}

View File

@ -121,6 +121,20 @@ pub fn (ce &CronExpression) next_from_now() ?time.Time {
return ce.next(time.now())
}
// next_n returns the n next occurences of the expression, given a starting
// time.
pub fn (ce &CronExpression) next_n(ref time.Time, n int) ?[]time.Time {
mut times := []time.Time{cap: n}
times << ce.next(ref)?
for i in 1 .. n {
times << ce.next(times[i - 1])?
}
return times
}
// parse_range parses a given string into a range of sorted integers, if
// possible.
fn parse_range(s string, min int, max int, mut bitv []bool) ? {