Skip to content

Commit

Permalink
Allow for "* 0-24 * * *", gh-30
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Aug 14, 2019
1 parent aea22a7 commit 707180c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions CREDITS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

# fugit credits

* Jessica Stokes https://github.com/ticky 0-24 issue with cron, gh-30
* Shai Coleman https://github.com/shaicoleman parse_nat enhancements, gh-24, gh-25, and gh-28
* Jan Stevens https://github.com/JanStevens Fugit.parse('every 15 minutes') gh-22
* Fabio Pitino https://github.com/hspazio nil on February 30 gh-21
Expand Down
15 changes: 10 additions & 5 deletions lib/fugit/cron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -497,23 +497,28 @@ def range(min, max, sta, edn, sla)

loop do

#p({ cur: cur })
a << cur
break if cur == edn

cur += 1
cur = min if cur > max
#p cur
if cur > max
cur = min
edn = edn - max - 1 if edn > max
end

fail RuntimeError.new(
"too many loops for " +
{ min: omin, max: omax, sta: sta, edn: edn, sla: sla }.inspect +
" #range, breaking, " +
"please fill an issue at https://git.io/fjJC9"
) if a.length > omax
) if a.length > 2 * omax
# there is a #uniq afterwards, hence the 2* for 0-24 and friends
end

a.each_with_index.select { |e, i| i % sla == 0 }.collect(&:first)
a.each_with_index
.select { |e, i| i % sla == 0 }
.collect(&:first)
.uniq
end

def compact(key)
Expand Down
21 changes: 19 additions & 2 deletions spec/cron_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
[ '0 0 * * mon#2,tue', '2017-01-09', '2017-01-06' ],
[ '0 0 * * mon#2,tue', '2017-01-31', '2017-01-30' ],

[ '00 24 * * *', '2017-01-02', '2017-01-01 12:00' ],
[ '00 24 * * *', '2017-01-02 00:00:00', '2017-01-01 12:00' ],

# Note: The day of a command's execution can be specified by two fields
# -- day of month, and day of week.
Expand Down Expand Up @@ -692,8 +692,19 @@ class Fugit::Cron::TimeCursor
[ '10-15 0 23 * * *', '10,11,12,13,14,15 0 23 * * *' ],
[ '58-2 0 23 * * *', '0,1,2,58,59 0 23 * * *' ],

[ '* 0-24 * * *', "* #{(0..23).to_a.collect(&:to_s).join(',')} * * *" ],
[ '* 22-24 * * *', '* 0,22,23 * * *' ],
[ '* * * 1-13 *', nil ], # month 13 isn't allowed at parsing
#
# gh-30

].each { |c, e|
it("parses #{c}") { expect(Fugit::Cron.parse(c).to_cron_s).to eq(e) }

it "parses #{c}" do

c = Fugit::Cron.parse(c)
expect(c ? c.to_cron_s : c).to eq(e)
end
}

context 'negative monthdays' do
Expand Down Expand Up @@ -985,6 +996,12 @@ class Fugit::Cron::TimeCursor
{ min: 1, max: 31, sta: -5, edn: -1, sla: 1 } => [ -5, -4, -3, -2, -1 ],
{ min: 1, max: 31, sta: -1, edn: -29, sla: 1 } => [ -1, -31, -30, -29 ],

{ min: 0, max: 23, sta: 0, edn: 24, sla: 1 } => (0..23).to_a,
#{ min: 1, max: 12, sta: 0, edn: 12, sla: 1 } => ...
#{ min: 1, max: 12, sta: 1, edn: 13, sla: 1 } => ... # month 13 no parse
#
# gh-30

}.each do |args, result|

it "returns #{result.inspect} for #{args.inspect}" do
Expand Down

0 comments on commit 707180c

Please sign in to comment.