Permalink
Browse files

Added support for `occurring_between?`

Closes #88
  • Loading branch information...
1 parent 0b8ee24 commit 8f93e7a4b40fe8122213cd84fff613f03a57836c @seejohnrun committed Oct 13, 2012
Showing with 53 additions and 1 deletion.
  1. +1 −0 README.md
  2. +7 −0 lib/ice_cube/schedule.rb
  3. +36 −1 spec/examples/recur_spec.rb
  4. +9 −0 spec/examples/regression_spec.rb
View
@@ -79,6 +79,7 @@ schedule.next_occurrences(3, [from_date])
schedule = Schedule.new(Time.now, :duration => 3600)
schedule.add_recurrence_rule Rule.daily
schedule.occurring_at?(Time.now + 1800) # true
+schedule.occurring_between?(t1, t2)
# you can also give schedules a solidified end_time
schedule = Schedule.new(Time.now, :end_time => Time.now + 3600)
@@ -170,6 +170,13 @@ 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)
+ dur = duration || 0
+ occurs_between?(begin_time - dur + 1, closing_time + dur - 1)
+ end
+
# Return a boolean indicating if an occurrence falls on a certain date
def occurs_on?(date)
begin_time = TimeUtil.beginning_of_date(date)
@@ -20,7 +20,42 @@
end
end
-
+
+describe :occurring_between? do
+
+ let(:start_time) { Time.local(2012, 7, 7, 7) }
+ 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
+ end
+
+ it 'should occur when the range is offset left' do
+ schedule.occurring_between?(start_time - 60, start_time - 29).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
+ end
+
+ it 'should occur when the range is overflowing' do
+ schedule.occurring_between?(start_time - 29, start_time + 40).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
+ 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
+ end
+
+end
+
describe :next_occurrence do
it 'should get the next occurrence from now' do
@@ -111,6 +111,15 @@
schedule.first(1)[0].mday.should_not == exdate.mday
end
+ it 'should return true if a recurring schedule occurs_between? a time range [#88]' do
+ start_time = Time.new(2012, 7, 7, 8)
+ schedule = IceCube::Schedule.new(start_time, :duration => 2 * IceCube::ONE_HOUR)
+ schedule.add_recurrence_rule Rule.weekly
+ t1 = Time.new(2012, 7, 14, 9)
+ t2 = Time.new(2012, 7, 14, 11)
+ schedule.occurring_between?(t1, t2).should be_true
+ end
+
require 'active_support/time'
it 'should not hang over DST [#53]' do

0 comments on commit 8f93e7a

Please sign in to comment.