Skip to content

Commit

Permalink
Implement Fugit::Cron#within(time_range)
Browse files Browse the repository at this point in the history
jmettraux committed Feb 16, 2024
1 parent 6d7419a commit 4b4bee6
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@

## fugit 1.10.0 not yet released

* Implement `Fugit::Cron#within(time_range)`
* Implement iterator-returning `Fugit::Cron#next` and `#prev`


7 changes: 7 additions & 0 deletions lib/fugit/cron.rb
Original file line number Diff line number Diff line change
@@ -345,6 +345,13 @@ def prev(from=::EtOrbi::EoTime.now)
CronIterator.new(self, :previous_time, from)
end

def within(time_range)

CronIterator
.new(self, :next_time, time_range.begin)
.take_while { |eot| eot.to_t < time_range.end }
end

# Mostly used as a #next_time sanity check.
# Avoid for "business" use, it's slow.
#
24 changes: 23 additions & 1 deletion spec/cron_spec.rb
Original file line number Diff line number Diff line change
@@ -1679,7 +1679,29 @@ class Fugit::Cron::TimeCursor
end
end

describe '#within'
describe '#within' do

it 'returns all the occurences within a given time period' do

c = Fugit.parse_cron('0 12 * * mon#2')

in_zone 'UTC' do

expect(
c
.within(
Time.parse('2024-02-16 12:00')..Time.parse('2024-08-01 12:00'))
.map(&:to_s)
).to eq([
'2024-03-11 12:00:00 Z',
'2024-04-08 12:00:00 Z',
'2024-05-13 12:00:00 Z',
'2024-06-10 12:00:00 Z',
'2024-07-08 12:00:00 Z'
])
end
end
end
end

describe Fugit do

0 comments on commit 4b4bee6

Please sign in to comment.