diff --git a/lib/ice_cube/schedule.rb b/lib/ice_cube/schedule.rb index a4176926..d23de964 100644 --- a/lib/ice_cube/schedule.rb +++ b/lib/ice_cube/schedule.rb @@ -182,16 +182,20 @@ def occurrences_between(begin_time, closing_time) find_occurrences(begin_time, closing_time) end - # Return a boolean indicating if an occurrence falls between - # two times + # Return a boolean indicating if an occurrence falls between two times def occurs_between?(begin_time, closing_time) !find_occurrences(begin_time, closing_time, 1).empty? end - # Return a boolean indicating if an occurrence is occurring between - # two times, inclusive - def occurring_between?(begin_time, closing_time) - occurs_between?(begin_time - duration + 1, closing_time + duration - 1) + # Return a boolean indicating if an occurrence is occurring between two + # times, inclusive of its duration. This counts zero-length occurrences + # that intersect the start of the range and within the range, but not + # occurrences at the end of the range since none of their duration + # intersects the range. + def occurring_between?(opening_time, closing_time) + opening_time = opening_time - duration + closing_time = closing_time - 1 if duration > 0 + occurs_between?(opening_time, closing_time) end # Return a boolean indicating if an occurrence falls on a certain date diff --git a/spec/examples/recur_spec.rb b/spec/examples/recur_spec.rb index ce89504e..38480bc7 100644 --- a/spec/examples/recur_spec.rb +++ b/spec/examples/recur_spec.rb @@ -24,34 +24,50 @@ describe :occurring_between? do let(:start_time) { Time.local(2012, 7, 7, 7) } + let(:end_time) { start_time + 30 } + let(:schedule) do IceCube::Schedule.new(start_time, :duration => 30).tap do |schedule| schedule.rrule IceCube::Rule.daily end end - it 'should occur when the range is entirely contained' do - schedule.occurring_between?(start_time + 1, start_time + 20).should be_true + it 'should affirm an occurrence that spans the range exactly' do + schedule.occurring_between?(start_time, end_time).should be_true + end + + it 'should affirm a zero-length occurrence at the start of the range' do + schedule.duration = 0 + schedule.occurring_between?(start_time, start_time).should be_true + end + + it 'should deny a zero-length occurrence at the end of the range' do + schedule.duration = 0 + schedule.occurring_between?(end_time, end_time).should be_false + end + + it 'should affirm an occurrence entirely contained within the range' do + schedule.occurring_between?(start_time + 1, end_time - 1).should be_true end - it 'should occur when the range is offset left' do - schedule.occurring_between?(start_time - 60, start_time - 29).should be_true + it 'should affirm an occurrence spanning across the start of the range' do + schedule.occurring_between?(start_time - 1, start_time + 1).should be_true end - it 'should occur when the range is offset right' do - schedule.occurring_between?(start_time + 29, start_time + 40).should be_true + it 'should affirm an occurrence spanning across the end of the range' do + schedule.occurring_between?(end_time - 1, end_time + 1).should be_true end - it 'should occur when the range is overflowing' do - schedule.occurring_between?(start_time - 29, start_time + 40).should be_true + it 'should affirm an occurrence spanning across the range entirely' do + schedule.occurring_between?(start_time - 1, end_time + 1).should be_true end - it 'should be false when the range starts after the duration expires' do - schedule.occurring_between?(start_time + 30, start_time + 40).should be_false + it 'should deny an occurrence before the range' do + schedule.occurring_between?(end_time + 1, end_time + 2).should be_false end - it 'should be false when the range ends before the start' do - schedule.occurring_between?(start_time - 40, start_time - 30).should be_false + it 'should deny an occurrence after the range' do + schedule.occurring_between?(start_time - 2, start_time - 1).should be_false end end