From feae0d65514269b13cb2bf8e4e3544ad658abd05 Mon Sep 17 00:00:00 2001 From: Gjergji Furxhi Date: Tue, 6 Oct 2020 16:42:22 +0000 Subject: [PATCH 1/2] Fix tests deprecations and style --- lib/cron_parser.rb | 69 +++++++++--------- spec/cron_parser_spec.rb | 146 ++++++++++++++++++++------------------- 2 files changed, 107 insertions(+), 108 deletions(-) diff --git a/lib/cron_parser.rb b/lib/cron_parser.rb index 41a8a5e..9c9b915 100644 --- a/lib/cron_parser.rb +++ b/lib/cron_parser.rb @@ -9,7 +9,7 @@ class InternalTime attr_accessor :year, :month, :day, :hour, :min attr_accessor :time_source - def initialize(time,time_source = Time) + def initialize(time, time_source = Time) @year = time.year @month = time.month @day = time.day @@ -29,29 +29,29 @@ def inspect end SYMBOLS = { - "jan" => "1", - "feb" => "2", - "mar" => "3", - "apr" => "4", - "may" => "5", - "jun" => "6", - "jul" => "7", - "aug" => "8", - "sep" => "9", - "oct" => "10", - "nov" => "11", - "dec" => "12", - - "sun" => "0", - "mon" => "1", - "tue" => "2", - "wed" => "3", - "thu" => "4", - "fri" => "5", - "sat" => "6" + "jan" => "1", + "feb" => "2", + "mar" => "3", + "apr" => "4", + "may" => "5", + "jun" => "6", + "jul" => "7", + "aug" => "8", + "sep" => "9", + "oct" => "10", + "nov" => "11", + "dec" => "12", + + "sun" => "0", + "mon" => "1", + "tue" => "2", + "wed" => "3", + "thu" => "4", + "fri" => "5", + "sat" => "6" } - def initialize(source,time_source = Time) + def initialize(source, time_source = Time) @source = interpret_vixieisms(source) @time_source = time_source validate_source @@ -76,7 +76,6 @@ def interpret_vixieisms(spec) end end - # returns the next occurence after the given date def next(now = @time_source.now, num = 1) t = InternalTime.new(now, @time_source) @@ -100,15 +99,15 @@ def next(now = @time_source.now, num = 1) nudge_minute(t) t = t.to_time if num > 1 - recursive_calculate(:next,t,num) + recursive_calculate(:next, t, num) else t end end # returns the last occurence before the given date - def last(now = @time_source.now, num=1) - t = InternalTime.new(now,@time_source) + def last(now = @time_source.now, num = 1) + t = InternalTime.new(now, @time_source) unless time_specs[:month][0].include?(t.month) nudge_month(t, :last) @@ -129,13 +128,12 @@ def last(now = @time_source.now, num=1) nudge_minute(t, :last) t = t.to_time if num > 1 - recursive_calculate(:last,t,num) + recursive_calculate(:last, t, num) else t end end - SUBELEMENT_REGEX = %r{^(\d+)(-(\d+)(/(\d+))?)?$} def parse_element(elem, allowed_range) values = elem.split(',').map do |subel| @@ -160,10 +158,9 @@ def parse_element(elem, allowed_range) [Set.new(values), values, elem] end - protected - def recursive_calculate(meth,time,num) + def recursive_calculate(meth, time, num) array = [time] num.-(1).times do |num| array << self.send(meth, array.last) @@ -249,11 +246,11 @@ def time_specs # tokens now contains the 5 fields tokens = substitute_parse_symbols(@source).split(/\s+/) { - :minute => parse_element(tokens[0], 0..59), #minute - :hour => parse_element(tokens[1], 0..23), #hour - :dom => parse_element(tokens[2], 1..31), #DOM - :month => parse_element(tokens[3], 1..12), #mon - :dow => parse_element(tokens[4], 0..6) #DOW + :minute => parse_element(tokens[0], 0..59), # minute + :hour => parse_element(tokens[1], 0..23), # hour + :dom => parse_element(tokens[2], 1..31), # DOM + :month => parse_element(tokens[3], 1..12), # mon + :dow => parse_element(tokens[4], 0..6) # DOW } end end @@ -264,7 +261,6 @@ def substitute_parse_symbols(str) end end - def stepped_range(rng, step = 1) len = rng.last - rng.first @@ -275,7 +271,6 @@ def stepped_range(rng, step = 1) result end - # returns the smallest element from allowed which is greater than current # returns nil if no matching value was found def find_best_next(current, allowed, dir) diff --git a/spec/cron_parser_spec.rb b/spec/cron_parser_spec.rb index 94f7ad0..048dab0 100644 --- a/spec/cron_parser_spec.rb +++ b/spec/cron_parser_spec.rb @@ -15,92 +15,96 @@ def parse_date(str) ["10", 0..59, [10]], ["10,30", 0..59, [10, 30]], ["10-15", 0..59, [10, 11, 12, 13, 14, 15]], - ["10-40/10", 0..59, [10, 20, 30, 40]], + ["10-40/10", 0..59, [10, 20, 30, 40]] ].each do |element, range, expected| it "should return #{expected} for '#{element}' when range is #{range}" do parser = CronParser.new('* * * * *') - parser.parse_element(element, range).first.to_a.sort.should == expected.sort + expect(parser.parse_element(element, range).first.to_a.sort).to eq(expected.sort) end end end describe "CronParser#next" do [ - ["* * * * *", "2011-08-15 12:00", "2011-08-15 12:01",1], - ["* * * * *", "2011-08-15 02:25", "2011-08-15 02:26",1], - ["* * * * *", "2011-08-15 02:59", "2011-08-15 03:00",1], - ["*/15 * * * *", "2011-08-15 02:02", "2011-08-15 02:15",1], - ["*/15,25 * * * *", "2011-08-15 02:15", "2011-08-15 02:25",1], - ["30 3,6,9 * * *", "2011-08-15 02:15", "2011-08-15 03:30",1], - ["30 9 * * *", "2011-08-15 10:15", "2011-08-16 09:30",1], - ["30 9 * * *", "2011-08-31 10:15", "2011-09-01 09:30",1], - ["30 9 * * *", "2011-09-30 10:15", "2011-10-01 09:30",1], - ["0 9 * * *", "2011-12-31 10:15", "2012-01-01 09:00",1], - ["* * 12 * *", "2010-04-15 10:15", "2010-05-12 00:00",1], - ["* * * * 1,3", "2010-04-15 10:15", "2010-04-19 00:00",1], - ["* * * * MON,WED", "2010-04-15 10:15", "2010-04-19 00:00",1], - ["0 0 1 1 *", "2010-04-15 10:15", "2011-01-01 00:00",1], - ["0 0 * * 1", "2011-08-01 00:00", "2011-08-08 00:00",1], - ["0 0 * * 1", "2011-07-25 00:00", "2011-08-01 00:00",1], - ["45 23 7 3 *", "2011-01-01 00:00", "2011-03-07 23:45",1], - ["0 0 1 jun *", "2013-05-14 11:20", "2013-06-01 00:00",1], - ["0 0 1 may,jul *", "2013-05-14 15:00", "2013-07-01 00:00",1], - ["0 0 1 MAY,JUL *", "2013-05-14 15:00", "2013-07-01 00:00",1], - ["40 5 * * *", "2014-02-01 15:56", "2014-02-02 05:40",1], - ["0 5 * * 1", "2014-02-01 15:56", "2014-02-03 05:00",1], - ["10 8 15 * *", "2014-02-01 15:56", "2014-02-15 08:10",1], - ["50 6 * * 1", "2014-02-01 15:56", "2014-02-03 06:50",1], - ["1 2 * apr mOn", "2014-02-01 15:56", "2014-04-07 02:01",1], - ["1 2 3 4 7", "2014-02-01 15:56", "2014-04-03 02:01",1], - ["1 2 3 4 7", "2014-04-04 15:56", "2014-04-06 02:01",1], - ["1-20/3 * * * *", "2014-02-01 15:56", "2014-02-01 16:01",1], - ["1,2,3 * * * *", "2014-02-01 15:56", "2014-02-01 16:01",1], - ["1-9,15-30 * * * *", "2014-02-01 15:56", "2014-02-01 16:01",1], - ["1-9/3,15-30/4 * * * *", "2014-02-01 15:56", "2014-02-01 16:01",1], - ["1 2 3 jan mon", "2014-02-01 15:56", "2015-01-03 02:01",1], - ["1 2 3 4 mON", "2014-02-01 15:56", "2014-04-03 02:01",1], - ["1 2 3 jan 5", "2014-02-01 15:56", "2015-01-02 02:01",1], - ["@yearly", "2014-02-01 15:56", "2015-01-01 00:00",1], - ["@annually", "2014-02-01 15:56", "2015-01-01 00:00",1], - ["@monthly", "2014-02-01 15:56", "2014-03-01 00:00",1], - ["@weekly", "2014-02-01 15:56", "2014-02-02 00:00",1], - ["@daily", "2014-02-01 15:56", "2014-02-02 00:00",1], - ["@midnight", "2014-02-01 15:56", "2014-02-02 00:00",1], - ["@hourly", "2014-02-01 15:56", "2014-02-01 16:00",1], - ["*/3 * * * *", "2014-02-01 15:56", "2014-02-01 15:57",1], - ["0 5 * 2,3 *", "2014-02-01 15:56", "2014-02-02 05:00",1], - ["15-59/15 * * * *", "2014-02-01 15:56", "2014-02-01 16:15",1], - ["15-59/15 * * * *", "2014-02-01 15:00", "2014-02-01 15:15",1], - ["15-59/15 * * * *", "2014-02-01 15:01", "2014-02-01 15:15",1], - ["15-59/15 * * * *", "2014-02-01 15:16", "2014-02-01 15:30",1], - ["15-59/15 * * * *", "2014-02-01 15:26", "2014-02-01 15:30",1], - ["15-59/15 * * * *", "2014-02-01 15:36", "2014-02-01 15:45",1], - ["15-59/15 * * * *", "2014-02-01 15:45", "2014-02-01 16:15",4], - ["15-59/15 * * * *", "2014-02-01 15:46", "2014-02-01 16:15",3], - ["15-59/15 * * * *", "2014-02-01 15:46", "2014-02-01 16:15",2], - ].each do |line, now, expected_next,num| + ["* * * * *", "2011-08-15 12:00", "2011-08-15 12:01", 1], + ["* * * * *", "2011-08-15 02:25", "2011-08-15 02:26", 1], + ["* * * * *", "2011-08-15 02:59", "2011-08-15 03:00", 1], + ["*/15 * * * *", "2011-08-15 02:02", "2011-08-15 02:15", 1], + ["*/15,25 * * * *", "2011-08-15 02:15", "2011-08-15 02:25", 1], + ["30 3,6,9 * * *", "2011-08-15 02:15", "2011-08-15 03:30", 1], + ["30 9 * * *", "2011-08-15 10:15", "2011-08-16 09:30", 1], + ["30 9 * * *", "2011-08-31 10:15", "2011-09-01 09:30", 1], + ["30 9 * * *", "2011-09-30 10:15", "2011-10-01 09:30", 1], + ["0 9 * * *", "2011-12-31 10:15", "2012-01-01 09:00", 1], + ["* * 12 * *", "2010-04-15 10:15", "2010-05-12 00:00", 1], + ["* * * * 1,3", "2010-04-15 10:15", "2010-04-19 00:00", 1], + ["* * * * MON,WED", "2010-04-15 10:15", "2010-04-19 00:00", 1], + ["0 0 1 1 *", "2010-04-15 10:15", "2011-01-01 00:00", 1], + ["0 0 * * 1", "2011-08-01 00:00", "2011-08-08 00:00", 1], + ["0 0 * * 1", "2011-07-25 00:00", "2011-08-01 00:00", 1], + ["45 23 7 3 *", "2011-01-01 00:00", "2011-03-07 23:45", 1], + ["0 0 1 jun *", "2013-05-14 11:20", "2013-06-01 00:00", 1], + ["0 0 1 may,jul *", "2013-05-14 15:00", "2013-07-01 00:00", 1], + ["0 0 1 MAY,JUL *", "2013-05-14 15:00", "2013-07-01 00:00", 1], + ["40 5 * * *", "2014-02-01 15:56", "2014-02-02 05:40", 1], + ["0 5 * * 1", "2014-02-01 15:56", "2014-02-03 05:00", 1], + ["10 8 15 * *", "2014-02-01 15:56", "2014-02-15 08:10", 1], + ["50 6 * * 1", "2014-02-01 15:56", "2014-02-03 06:50", 1], + ["1 2 * apr mOn", "2014-02-01 15:56", "2014-04-07 02:01", 1], + ["1 2 3 4 7", "2014-02-01 15:56", "2014-04-03 02:01", 1], + ["1 2 3 4 7", "2014-04-04 15:56", "2014-04-06 02:01", 1], + ["1-20/3 * * * *", "2014-02-01 15:56", "2014-02-01 16:01", 1], + ["1,2,3 * * * *", "2014-02-01 15:56", "2014-02-01 16:01", 1], + ["1-9,15-30 * * * *", "2014-02-01 15:56", "2014-02-01 16:01", 1], + ["1-9/3,15-30/4 * * * *", "2014-02-01 15:56", "2014-02-01 16:01", 1], + ["1 2 3 jan mon", "2014-02-01 15:56", "2015-01-03 02:01", 1], + ["1 2 3 4 mON", "2014-02-01 15:56", "2014-04-03 02:01", 1], + ["1 2 3 jan 5", "2014-02-01 15:56", "2015-01-02 02:01", 1], + ["@yearly", "2014-02-01 15:56", "2015-01-01 00:00", 1], + ["@annually", "2014-02-01 15:56", "2015-01-01 00:00", 1], + ["@monthly", "2014-02-01 15:56", "2014-03-01 00:00", 1], + ["@weekly", "2014-02-01 15:56", "2014-02-02 00:00", 1], + ["@daily", "2014-02-01 15:56", "2014-02-02 00:00", 1], + ["@midnight", "2014-02-01 15:56", "2014-02-02 00:00", 1], + ["@hourly", "2014-02-01 15:56", "2014-02-01 16:00", 1], + ["*/3 * * * *", "2014-02-01 15:56", "2014-02-01 15:57", 1], + ["0 5 * 2,3 *", "2014-02-01 15:56", "2014-02-02 05:00", 1], + ["15-59/15 * * * *", "2014-02-01 15:56", "2014-02-01 16:15", 1], + ["15-59/15 * * * *", "2014-02-01 15:00", "2014-02-01 15:15", 1], + ["15-59/15 * * * *", "2014-02-01 15:01", "2014-02-01 15:15", 1], + ["15-59/15 * * * *", "2014-02-01 15:16", "2014-02-01 15:30", 1], + ["15-59/15 * * * *", "2014-02-01 15:26", "2014-02-01 15:30", 1], + ["15-59/15 * * * *", "2014-02-01 15:36", "2014-02-01 15:45", 1], + ["15-59/15 * * * *", "2014-02-01 15:45", "2014-02-01 16:15", 4], + ["15-59/15 * * * *", "2014-02-01 15:46", "2014-02-01 16:15", 3], + ["15-59/15 * * * *", "2014-02-01 15:46", "2014-02-01 16:15", 2] + ].each do |line, now, expected_next, num| it "returns #{expected_next} for '#{line}' when now is #{now}" do parsed_now = parse_date(now) expected = parse_date(expected_next) + parser = CronParser.new(line) - parser.next(parsed_now).xmlschema.should == expected.xmlschema + + expect(parser.next(parsed_now).xmlschema).to eq(expected.xmlschema) end it "returns the expected class" do parsed_now = parse_date(now) - expected = parse_date(expected_next) + parser = CronParser.new(line) - result = parser.next(parsed_now,num) - result.class.to_s.should == (num > 1 ? 'Array' : 'Time') + + result = parser.next(parsed_now, num) + expect(result.class.to_s).to eq((num > 1 ? 'Array' : 'Time')) end it "returns the expected count" do parsed_now = parse_date(now) - expected = parse_date(expected_next) + parser = CronParser.new(line) - result = parser.next(parsed_now,num) + + result = parser.next(parsed_now, num) if result.class.to_s == 'Array' - result.size.should == num + expect(result.size).to eq(num) else - result.class.to_s.should == 'Time' + expect(result.class.to_s).to eq('Time') end end end @@ -156,15 +160,15 @@ def parse_date(str) ["15-59/15 * * * *", "2014-02-01 15:26", "2014-02-01 15:15"], ["15-59/15 * * * *", "2014-02-01 15:36", "2014-02-01 15:30"], ["15-59/15 * * * *", "2014-02-01 15:45", "2014-02-01 15:30"], - ["15-59/15 * * * *", "2014-02-01 15:46", "2014-02-01 15:45"], - ].each do |line, now, expected_next| - it "should return #{expected_next} for '#{line}' when now is #{now}" do - now = parse_date(now) - expected_next = parse_date(expected_next) + ["15-59/15 * * * *", "2014-02-01 15:46", "2014-02-01 15:45"] + ].each do |line, now, expected_last| + it "should return #{expected_last} for '#{line}' when now is #{now}" do + parsed_now = parse_date(now) + expected = parse_date(expected_last) parser = CronParser.new(line) - parser.last(now).should == expected_next + expect(parser.last(parsed_now)).to eq(expected) end end end @@ -182,7 +186,7 @@ def parse_date(str) describe "time source" do it "should use an alternate specified time source" do ExtendedTime = Class.new(Time) - ExtendedTime.should_receive(:local).once - CronParser.new("* * * * *",ExtendedTime).next + allow(ExtendedTime).to receive(:local).once + CronParser.new("* * * * *", ExtendedTime).next end end From 45ebae3ba36420c7e9518750527385d323bf63ee Mon Sep 17 00:00:00 2001 From: Gjergji Furxhi Date: Wed, 7 Oct 2020 13:51:17 +0000 Subject: [PATCH 2/2] Fix minute approximation --- lib/cron_parser.rb | 1 + lib/parse-cron/version.rb | 2 +- spec/cron_parser_spec.rb | 204 +++++++++++++++++++------------------- 3 files changed, 105 insertions(+), 102 deletions(-) diff --git a/lib/cron_parser.rb b/lib/cron_parser.rb index 9c9b915..526f6b4 100644 --- a/lib/cron_parser.rb +++ b/lib/cron_parser.rb @@ -235,6 +235,7 @@ def nudge_hour(t, dir = :next) def nudge_minute(t, dir = :next) spec = time_specs[:minute][1] + t.min = t.min + 1 unless dir == :next next_value = find_best_next(t.min, spec, dir) t.min = next_value || (dir == :next ? spec.first : spec.last) diff --git a/lib/parse-cron/version.rb b/lib/parse-cron/version.rb index c256e8e..3718610 100644 --- a/lib/parse-cron/version.rb +++ b/lib/parse-cron/version.rb @@ -1,5 +1,5 @@ module Parse module Cron - VERSION = "0.1.4" + VERSION = "0.1.5" end end diff --git a/spec/cron_parser_spec.rb b/spec/cron_parser_spec.rb index 048dab0..fe9e3da 100644 --- a/spec/cron_parser_spec.rb +++ b/spec/cron_parser_spec.rb @@ -26,58 +26,58 @@ def parse_date(str) describe "CronParser#next" do [ - ["* * * * *", "2011-08-15 12:00", "2011-08-15 12:01", 1], - ["* * * * *", "2011-08-15 02:25", "2011-08-15 02:26", 1], - ["* * * * *", "2011-08-15 02:59", "2011-08-15 03:00", 1], - ["*/15 * * * *", "2011-08-15 02:02", "2011-08-15 02:15", 1], - ["*/15,25 * * * *", "2011-08-15 02:15", "2011-08-15 02:25", 1], - ["30 3,6,9 * * *", "2011-08-15 02:15", "2011-08-15 03:30", 1], - ["30 9 * * *", "2011-08-15 10:15", "2011-08-16 09:30", 1], - ["30 9 * * *", "2011-08-31 10:15", "2011-09-01 09:30", 1], - ["30 9 * * *", "2011-09-30 10:15", "2011-10-01 09:30", 1], - ["0 9 * * *", "2011-12-31 10:15", "2012-01-01 09:00", 1], - ["* * 12 * *", "2010-04-15 10:15", "2010-05-12 00:00", 1], - ["* * * * 1,3", "2010-04-15 10:15", "2010-04-19 00:00", 1], - ["* * * * MON,WED", "2010-04-15 10:15", "2010-04-19 00:00", 1], - ["0 0 1 1 *", "2010-04-15 10:15", "2011-01-01 00:00", 1], - ["0 0 * * 1", "2011-08-01 00:00", "2011-08-08 00:00", 1], - ["0 0 * * 1", "2011-07-25 00:00", "2011-08-01 00:00", 1], - ["45 23 7 3 *", "2011-01-01 00:00", "2011-03-07 23:45", 1], - ["0 0 1 jun *", "2013-05-14 11:20", "2013-06-01 00:00", 1], - ["0 0 1 may,jul *", "2013-05-14 15:00", "2013-07-01 00:00", 1], - ["0 0 1 MAY,JUL *", "2013-05-14 15:00", "2013-07-01 00:00", 1], - ["40 5 * * *", "2014-02-01 15:56", "2014-02-02 05:40", 1], - ["0 5 * * 1", "2014-02-01 15:56", "2014-02-03 05:00", 1], - ["10 8 15 * *", "2014-02-01 15:56", "2014-02-15 08:10", 1], - ["50 6 * * 1", "2014-02-01 15:56", "2014-02-03 06:50", 1], - ["1 2 * apr mOn", "2014-02-01 15:56", "2014-04-07 02:01", 1], - ["1 2 3 4 7", "2014-02-01 15:56", "2014-04-03 02:01", 1], - ["1 2 3 4 7", "2014-04-04 15:56", "2014-04-06 02:01", 1], - ["1-20/3 * * * *", "2014-02-01 15:56", "2014-02-01 16:01", 1], - ["1,2,3 * * * *", "2014-02-01 15:56", "2014-02-01 16:01", 1], - ["1-9,15-30 * * * *", "2014-02-01 15:56", "2014-02-01 16:01", 1], - ["1-9/3,15-30/4 * * * *", "2014-02-01 15:56", "2014-02-01 16:01", 1], - ["1 2 3 jan mon", "2014-02-01 15:56", "2015-01-03 02:01", 1], - ["1 2 3 4 mON", "2014-02-01 15:56", "2014-04-03 02:01", 1], - ["1 2 3 jan 5", "2014-02-01 15:56", "2015-01-02 02:01", 1], - ["@yearly", "2014-02-01 15:56", "2015-01-01 00:00", 1], - ["@annually", "2014-02-01 15:56", "2015-01-01 00:00", 1], - ["@monthly", "2014-02-01 15:56", "2014-03-01 00:00", 1], - ["@weekly", "2014-02-01 15:56", "2014-02-02 00:00", 1], - ["@daily", "2014-02-01 15:56", "2014-02-02 00:00", 1], - ["@midnight", "2014-02-01 15:56", "2014-02-02 00:00", 1], - ["@hourly", "2014-02-01 15:56", "2014-02-01 16:00", 1], - ["*/3 * * * *", "2014-02-01 15:56", "2014-02-01 15:57", 1], - ["0 5 * 2,3 *", "2014-02-01 15:56", "2014-02-02 05:00", 1], - ["15-59/15 * * * *", "2014-02-01 15:56", "2014-02-01 16:15", 1], - ["15-59/15 * * * *", "2014-02-01 15:00", "2014-02-01 15:15", 1], - ["15-59/15 * * * *", "2014-02-01 15:01", "2014-02-01 15:15", 1], - ["15-59/15 * * * *", "2014-02-01 15:16", "2014-02-01 15:30", 1], - ["15-59/15 * * * *", "2014-02-01 15:26", "2014-02-01 15:30", 1], - ["15-59/15 * * * *", "2014-02-01 15:36", "2014-02-01 15:45", 1], - ["15-59/15 * * * *", "2014-02-01 15:45", "2014-02-01 16:15", 4], - ["15-59/15 * * * *", "2014-02-01 15:46", "2014-02-01 16:15", 3], - ["15-59/15 * * * *", "2014-02-01 15:46", "2014-02-01 16:15", 2] + ["* * * * *", "2011-08-15 12:00:15", "2011-08-15 12:01:00", 1], + ["* * * * *", "2011-08-15 02:25:30", "2011-08-15 02:26:00", 1], + ["* * * * *", "2011-08-15 02:59:45", "2011-08-15 03:00:00", 1], + ["*/15 * * * *", "2011-08-15 02:02:55", "2011-08-15 02:15:00", 1], + ["*/15,25 * * * *", "2011-08-15 02:15:01", "2011-08-15 02:25:00", 1], + ["30 3,6,9 * * *", "2011-08-15 02:15:34", "2011-08-15 03:30:00", 1], + ["30 9 * * *", "2011-08-15 10:15:22", "2011-08-16 09:30:00", 1], + ["30 9 * * *", "2011-08-31 10:15:59", "2011-09-01 09:30:00", 1], + ["30 9 * * *", "2011-09-30 10:15:12", "2011-10-01 09:30:00", 1], + ["0 9 * * *", "2011-12-31 10:15:45", "2012-01-01 09:00:00", 1], + ["* * 12 * *", "2010-04-15 10:15:33", "2010-05-12 00:00:00", 1], + ["* * * * 1,3", "2010-04-15 10:15:12", "2010-04-19 00:00:00", 1], + ["* * * * MON,WED", "2010-04-15 10:15:00", "2010-04-19 00:00:00", 1], + ["0 0 1 1 *", "2010-04-15 10:15:21", "2011-01-01 00:00:00", 1], + ["0 0 * * 1", "2011-08-01 00:00:00", "2011-08-08 00:00:00", 1], + ["0 0 * * 1", "2011-07-25 00:00:19", "2011-08-01 00:00:00", 1], + ["45 23 7 3 *", "2011-01-01 00:00:11", "2011-03-07 23:45:00", 1], + ["0 0 1 jun *", "2013-05-14 11:20:32", "2013-06-01 00:00:00", 1], + ["0 0 1 may,jul *", "2013-05-14 15:00:00", "2013-07-01 00:00:00", 1], + ["0 0 1 MAY,JUL *", "2013-05-14 15:00:00", "2013-07-01 00:00:00", 1], + ["40 5 * * *", "2014-02-01 15:56:54", "2014-02-02 05:40:00", 1], + ["0 5 * * 1", "2014-02-01 15:56:34", "2014-02-03 05:00:00", 1], + ["10 8 15 * *", "2014-02-01 15:56:12", "2014-02-15 08:10:00", 1], + ["50 6 * * 1", "2014-02-01 15:56:43", "2014-02-03 06:50:00", 1], + ["1 2 * apr mOn", "2014-02-01 15:56:11", "2014-04-07 02:01:00", 1], + ["1 2 3 4 7", "2014-02-01 15:56:44", "2014-04-03 02:01:00", 1], + ["1 2 3 4 7", "2014-04-04 15:56:00", "2014-04-06 02:01:00", 1], + ["1-20/3 * * * *", "2014-02-01 15:56:44", "2014-02-01 16:01:00", 1], + ["1,2,3 * * * *", "2014-02-01 15:56:43", "2014-02-01 16:01:00", 1], + ["1-9,15-30 * * * *", "2014-02-01 15:56:12", "2014-02-01 16:01:00", 1], + ["1-9/3,15-30/4 * * * *", "2014-02-01 15:56:00", "2014-02-01 16:01:00", 1], + ["1 2 3 jan mon", "2014-02-01 15:56:13", "2015-01-03 02:01:00", 1], + ["1 2 3 4 mON", "2014-02-01 15:56:00", "2014-04-03 02:01:00", 1], + ["1 2 3 jan 5", "2014-02-01 15:56:26", "2015-01-02 02:01:00", 1], + ["@yearly", "2014-02-01 15:56:00", "2015-01-01 00:00:00", 1], + ["@annually", "2014-02-01 15:56:37", "2015-01-01 00:00:00", 1], + ["@monthly", "2014-02-01 15:56:14", "2014-03-01 00:00:00", 1], + ["@weekly", "2014-02-01 15:56:00", "2014-02-02 00:00:00", 1], + ["@daily", "2014-02-01 15:56:23", "2014-02-02 00:00:00", 1], + ["@midnight", "2014-02-01 15:56:17", "2014-02-02 00:00:00", 1], + ["@hourly", "2014-02-01 15:56:00", "2014-02-01 16:00:00", 1], + ["*/3 * * * *", "2014-02-01 15:56:00", "2014-02-01 15:57:00", 1], + ["0 5 * 2,3 *", "2014-02-01 15:56:19", "2014-02-02 05:00:00", 1], + ["15-59/15 * * * *", "2014-02-01 15:56:00", "2014-02-01 16:15:00", 1], + ["15-59/15 * * * *", "2014-02-01 15:00:00", "2014-02-01 15:15:00", 1], + ["15-59/15 * * * *", "2014-02-01 15:01:21", "2014-02-01 15:15:00", 1], + ["15-59/15 * * * *", "2014-02-01 15:16:22", "2014-02-01 15:30:00", 1], + ["15-59/15 * * * *", "2014-02-01 15:26:00", "2014-02-01 15:30:00", 1], + ["15-59/15 * * * *", "2014-02-01 15:36:38", "2014-02-01 15:45:00", 1], + ["15-59/15 * * * *", "2014-02-01 15:45:00", "2014-02-01 16:15:00", 4], + ["15-59/15 * * * *", "2014-02-01 15:46:36", "2014-02-01 16:15:00", 3], + ["15-59/15 * * * *", "2014-02-01 15:46:00", "2014-02-01 16:15:00", 2] ].each do |line, now, expected_next, num| it "returns #{expected_next} for '#{line}' when now is #{now}" do parsed_now = parse_date(now) @@ -112,55 +112,57 @@ def parse_date(str) describe "CronParser#last" do [ - ["* * * * *", "2011-08-15 12:00", "2011-08-15 11:59"], - ["* * * * *", "2011-08-15 02:25", "2011-08-15 02:24"], - ["* * * * *", "2011-08-15 03:00", "2011-08-15 02:59"], - ["*/15 * * * *", "2011-08-15 02:02", "2011-08-15 02:00"], - ["*/15,45 * * * *", "2011-08-15 02:55", "2011-08-15 02:45"], - ["*/15,25 * * * *", "2011-08-15 02:35", "2011-08-15 02:30"], - ["30 3,6,9 * * *", "2011-08-15 02:15", "2011-08-14 09:30"], - ["30 9 * * *", "2011-08-15 10:15", "2011-08-15 09:30"], - ["30 9 * * *", "2011-09-01 08:15", "2011-08-31 09:30"], - ["30 9 * * *", "2011-10-01 08:15", "2011-09-30 09:30"], - ["0 9 * * *", "2012-01-01 00:15", "2011-12-31 09:00"], - ["* * 12 * *", "2010-04-15 10:15", "2010-04-12 23:59"], - ["* * * * 1,3", "2010-04-15 10:15", "2010-04-14 23:59"], - ["* * * * MON,WED", "2010-04-15 10:15", "2010-04-14 23:59"], - ["0 0 1 1 *", "2010-04-15 10:15", "2010-01-01 00:00"], - ["0 0 1 jun *", "2013-05-14 11:20", "2012-06-01 00:00"], - ["0 0 1 may,jul *", "2013-05-14 15:00", "2013-05-01 00:00"], - ["0 0 1 MAY,JUL *", "2013-05-14 15:00", "2013-05-01 00:00"], - ["40 5 * * *", "2014-02-01 15:56", "2014-02-01 05:40"], - ["0 5 * * 1", "2014-02-01 15:56", "2014-01-27 05:00"], - ["10 8 15 * *", "2014-02-01 15:56", "2014-01-15 08:10"], - ["50 6 * * 1", "2014-02-01 15:56", "2014-01-27 06:50"], - ["1 2 * apr mOn", "2014-02-01 15:56", "2013-04-29 02:01"], - ["1 2 3 4 7", "2014-02-01 15:56", "2013-04-28 02:01"], - ["1 2 3 4 7", "2014-04-04 15:56", "2014-04-03 02:01"], - ["1-20/3 * * * *", "2014-02-01 15:56", "2014-02-01 15:19"], - ["1,2,3 * * * *", "2014-02-01 15:56", "2014-02-01 15:03"], - ["1-9,15-30 * * * *", "2014-02-01 15:56", "2014-02-01 15:30"], - ["1-9/3,15-30/4 * * * *", "2014-02-01 15:56", "2014-02-01 15:27"], - ["1 2 3 jan mon", "2014-02-01 15:56", "2014-01-27 02:01"], - ["1 2 3 4 mON", "2014-02-01 15:56", "2013-04-29 02:01"], - ["1 2 3 jan 5", "2014-02-01 15:56", "2014-01-31 02:01"], - ["@yearly", "2014-02-01 15:56", "2014-01-01 00:00"], - ["@annually", "2014-02-01 15:56", "2014-01-01 00:00"], - ["@monthly", "2014-02-01 15:56", "2014-02-01 00:00"], - ["@weekly", "2014-02-01 15:56", "2014-01-26 00:00"], - ["@daily", "2014-02-01 15:56", "2014-02-01 00:00"], - ["@midnight", "2014-02-01 15:56", "2014-02-01 00:00"], - ["@hourly", "2014-02-01 15:56", "2014-02-01 15:00"], - ["*/3 * * * *", "2014-02-01 15:56", "2014-02-01 15:54"], - ["0 5 * 2,3 *", "2014-02-01 15:56", "2014-02-01 05:00"], - ["15-59/15 * * * *", "2014-02-01 15:56", "2014-02-01 15:45"], - ["15-59/15 * * * *", "2014-02-01 15:00", "2014-02-01 14:45"], - ["15-59/15 * * * *", "2014-02-01 15:01", "2014-02-01 14:45"], - ["15-59/15 * * * *", "2014-02-01 15:16", "2014-02-01 15:15"], - ["15-59/15 * * * *", "2014-02-01 15:26", "2014-02-01 15:15"], - ["15-59/15 * * * *", "2014-02-01 15:36", "2014-02-01 15:30"], - ["15-59/15 * * * *", "2014-02-01 15:45", "2014-02-01 15:30"], - ["15-59/15 * * * *", "2014-02-01 15:46", "2014-02-01 15:45"] + ["* * * * *", "2011-08-15 12:00:00", "2011-08-15 12:00:00"], + ["* * * * *", "2011-08-15 02:25:13", "2011-08-15 02:25:00"], + ["* * * * *", "2011-08-15 03:00:59", "2011-08-15 03:00:00"], + ["*/15 * * * *", "2011-08-15 02:02:00", "2011-08-15 02:00:00"], + ["*/15 * * * *", "2011-08-15 02:15:01", "2011-08-15 02:15:00"], + ["*/15,45 * * * *", "2011-08-15 02:55:00", "2011-08-15 02:45:00"], + ["*/15,25 * * * *", "2011-08-15 02:35:00", "2011-08-15 02:30:00"], + ["30 3,6,9 * * *", "2011-08-15 02:15:00", "2011-08-14 09:30:00"], + ["30 9 * * *", "2011-08-15 10:15:00", "2011-08-15 09:30:00"], + ["30 9 * * *", "2011-09-01 08:15:00", "2011-08-31 09:30:00"], + ["30 9 * * *", "2011-10-01 08:15:00", "2011-09-30 09:30:00"], + ["30 9 * * *", "2011-10-01 09:30:11", "2011-10-01 09:30:00"], + ["0 9 * * *", "2012-01-01 00:15:00", "2011-12-31 09:00:00"], + ["* * 12 * *", "2010-04-15 10:15:00", "2010-04-12 23:59:00"], + ["* * * * 1,3", "2010-04-15 10:15:00", "2010-04-14 23:59:00"], + ["* * * * MON,WED", "2010-04-15 10:15:00", "2010-04-14 23:59:00"], + ["0 0 1 1 *", "2010-04-15 10:15:00", "2010-01-01 00:00:00"], + ["0 0 1 jun *", "2013-05-14 11:20:00", "2012-06-01 00:00:00"], + ["0 0 1 may,jul *", "2013-05-14 15:00:00", "2013-05-01 00:00:00"], + ["0 0 1 MAY,JUL *", "2013-05-14 15:00:00", "2013-05-01 00:00:00"], + ["40 5 * * *", "2014-02-01 15:56:00", "2014-02-01 05:40:00"], + ["0 5 * * 1", "2014-02-01 15:56:00", "2014-01-27 05:00:00"], + ["10 8 15 * *", "2014-02-01 15:56:00", "2014-01-15 08:10:00"], + ["50 6 * * 1", "2014-02-01 15:56:00", "2014-01-27 06:50:00"], + ["1 2 * apr mOn", "2014-02-01 15:56:00", "2013-04-29 02:01:00"], + ["1 2 3 4 7", "2014-02-01 15:56:00", "2013-04-28 02:01:00"], + ["1 2 3 4 7", "2014-04-04 15:56:00", "2014-04-03 02:01:00"], + ["1-20/3 * * * *", "2014-02-01 15:56:00", "2014-02-01 15:19:00"], + ["1,2,3 * * * *", "2014-02-01 15:56:00", "2014-02-01 15:03:00"], + ["1-9,15-30 * * * *", "2014-02-01 15:56:00", "2014-02-01 15:30:00"], + ["1-9/3,15-30/4 * * * *", "2014-02-01 15:56:00", "2014-02-01 15:27:00"], + ["1 2 3 jan mon", "2014-02-01 15:56:00", "2014-01-27 02:01:00"], + ["1 2 3 4 mON", "2014-02-01 15:56:00", "2013-04-29 02:01:00"], + ["1 2 3 jan 5", "2014-02-01 15:56:00", "2014-01-31 02:01:00"], + ["@yearly", "2014-02-01 15:56:00", "2014-01-01 00:00:00"], + ["@annually", "2014-02-01 15:56:00", "2014-01-01 00:00:00"], + ["@monthly", "2014-02-01 15:56:00", "2014-02-01 00:00:00"], + ["@weekly", "2014-02-01 15:56:00", "2014-01-26 00:00:00"], + ["@daily", "2014-02-01 15:56:00", "2014-02-01 00:00:00"], + ["@midnight", "2014-02-01 15:56:00", "2014-02-01 00:00:00"], + ["@hourly", "2014-02-01 15:56:00", "2014-02-01 15:00:00"], + ["*/3 * * * *", "2014-02-01 15:56:00", "2014-02-01 15:54:00"], + ["0 5 * 2,3 *", "2014-02-01 15:56:00", "2014-02-01 05:00:00"], + ["15-59/15 * * * *", "2014-02-01 15:56:00", "2014-02-01 15:45:00"], + ["15-59/15 * * * *", "2014-02-01 15:00:00", "2014-02-01 14:45:00"], + ["15-59/15 * * * *", "2014-02-01 15:01:00", "2014-02-01 14:45:00"], + ["15-59/15 * * * *", "2014-02-01 15:16:00", "2014-02-01 15:15:00"], + ["15-59/15 * * * *", "2014-02-01 15:26:00", "2014-02-01 15:15:00"], + ["15-59/15 * * * *", "2014-02-01 15:36:00", "2014-02-01 15:30:00"], + ["15-59/15 * * * *", "2014-02-01 15:45:00", "2014-02-01 15:45:00"], + ["15-59/15 * * * *", "2014-02-01 15:46:00", "2014-02-01 15:45:00"] ].each do |line, now, expected_last| it "should return #{expected_last} for '#{line}' when now is #{now}" do parsed_now = parse_date(now)