refactor: added CronExpression.next_n function
							parent
							
								
									5e07b0efc9
								
							
						
					
					
						commit
						281e4bcaf1
					
				|  | @ -2,11 +2,13 @@ module schedule | ||||||
| 
 | 
 | ||||||
| import cli | import cli | ||||||
| import cron.expression { parse_expression } | import cron.expression { parse_expression } | ||||||
|  | import time | ||||||
| 
 | 
 | ||||||
| // cmd returns the cli submodule for previewing a cron schedule. | // cmd returns the cli submodule for previewing a cron schedule. | ||||||
| pub fn cmd() cli.Command { | pub fn cmd() cli.Command { | ||||||
| 	return cli.Command{ | 	return cli.Command{ | ||||||
| 		name: 'schedule' | 		name: 'schedule' | ||||||
|  | 		usage: 'schedule' | ||||||
| 		description: 'Preview the behavior of a cron schedule.' | 		description: 'Preview the behavior of a cron schedule.' | ||||||
| 		flags: [ | 		flags: [ | ||||||
| 			cli.Flag{ | 			cli.Flag{ | ||||||
|  | @ -17,16 +19,10 @@ pub fn cmd() cli.Command { | ||||||
| 			}, | 			}, | ||||||
| 		] | 		] | ||||||
| 		execute: fn (cmd cli.Command) ? { | 		execute: fn (cmd cli.Command) ? { | ||||||
| 			exp := parse_expression(cmd.args.join(' '))? | 			ce := parse_expression(cmd.args.join(' '))? | ||||||
| 
 |  | ||||||
| 			mut t := exp.next_from_now()? |  | ||||||
| 			println(t) |  | ||||||
| 
 |  | ||||||
| 			count := cmd.flags.get_int('count')? | 			count := cmd.flags.get_int('count')? | ||||||
| 
 | 
 | ||||||
| 			for _ in 1 .. count { | 			for t in ce.next_n(time.now(), count)? { | ||||||
| 				t = exp.next(t)? |  | ||||||
| 
 |  | ||||||
| 				println(t) | 				println(t) | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | @ -121,6 +121,20 @@ pub fn (ce &CronExpression) next_from_now() ?time.Time { | ||||||
| 	return ce.next(time.now()) | 	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 | // parse_range parses a given string into a range of sorted integers, if | ||||||
| // possible. | // possible. | ||||||
| fn parse_range(s string, min int, max int, mut bitv []bool) ? { | fn parse_range(s string, min int, max int, mut bitv []bool) ? { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue