Skip to content

Commit

Permalink
Let Cron#within accept start + end time
Browse files Browse the repository at this point in the history
instead of just a time Range instance
  • Loading branch information
jmettraux committed Feb 16, 2024
1 parent f514297 commit 358743f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
10 changes: 7 additions & 3 deletions lib/fugit/cron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,15 @@ def prev(from=::EtOrbi::EoTime.now)
# Returns an array of EtOrbi::EoTime instances that correspond to
# the occurrences of the cron within the given time range
#
def within(time_range)
def within(time_range, time_end=nil)

sta, ned =
time_range.is_a?(::Range) ? [ time_range.begin, time_range.end ] :
[ ::EtOrbi.make_time(time_range), ::EtOrbi.make_time(time_end) ]

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

# Mostly used as a #next_time sanity check.
Expand Down
28 changes: 25 additions & 3 deletions spec/cron_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,7 @@ class Fugit::Cron::TimeCursor

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

in_zone 'UTC' do
in_zone 'UTC' do

expect(
#c.next(Time.parse('2024-02-16 12:00:00'))
Expand All @@ -1662,7 +1662,7 @@ class Fugit::Cron::TimeCursor

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

in_zone 'UTC' do
in_zone 'UTC' do

expect(
c.prev(Time.parse('2024-02-16 12:00:00'))
Expand All @@ -1685,7 +1685,7 @@ class Fugit::Cron::TimeCursor

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

in_zone 'UTC' do
in_zone 'UTC' do

expect(
c
Expand All @@ -1701,6 +1701,28 @@ class Fugit::Cron::TimeCursor
])
end
end

it 'returns all the occurences within a start time and an end time' do

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

in_zone 'UTC' do

expect(
c
.within(
Time.parse('2024-01-16 12:00'),
'2024-07-01 12:00') # EtOrbi.make_time is used...
.map(&:to_s)
).to eq([
'2024-02-12 12:00:00 Z',
'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'
])
end
end
end
end

Expand Down

0 comments on commit 358743f

Please sign in to comment.