Skip to content

Commit e14ad76

Browse files
committed
End of duration shouldn't hit start of check
The spans checks were incorrectly returning a hit when the end of the duration for an event ended on the same time as the start time checks. Allows occurring_between? method to now be simplifed to use the spanned check.
1 parent d49c4a4 commit e14ad76

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

lib/ice_cube/schedule.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,7 @@ def occurs_between?(begin_time, closing_time, spans = false)
226226
# occurrences at the end of the range since none of their duration
227227
# intersects the range.
228228
def occurring_between?(opening_time, closing_time)
229-
opening_time = opening_time - duration
230-
closing_time = closing_time - 1 if duration > 0
231-
occurs_between?(opening_time, closing_time)
229+
occurs_between?(opening_time, closing_time, true)
232230
end
233231

234232
# Return a boolean indicating if an occurrence falls on a certain date
@@ -409,13 +407,14 @@ def enumerate_occurrences(opening_time, closing_time = nil, spans = false, &bloc
409407
closing_time = TimeUtil.match_zone(closing_time, start_time)
410408
opening_time += start_time.subsec - opening_time.subsec rescue 0
411409
opening_time = start_time if opening_time < start_time
410+
spans = false if duration == 0
412411
Enumerator.new do |yielder|
413412
reset
414413
t1 = full_required? ? start_time : realign((spans ? opening_time - duration : opening_time))
415414
loop do
416415
break unless (t0 = next_time(t1, closing_time))
417416
break if closing_time && t0 > closing_time
418-
if (spans ? t0.end_time : t0) >= opening_time
417+
if (spans ? (t0.end_time > opening_time) : (t0 >= opening_time))
419418
yielder << (block_given? ? block.call(t0) : t0)
420419
end
421420
break unless (t1 = next_time(t0 + 1, closing_time))
@@ -424,7 +423,7 @@ def enumerate_occurrences(opening_time, closing_time = nil, spans = false, &bloc
424423
wind_back_dst
425424
next (t1 += 1)
426425
end
427-
if (spans ? t1.end_time : t1) >= opening_time
426+
if (spans ? (t1.end_time > opening_time) : (t1 >= opening_time))
428427
yielder << (block_given? ? block.call(t1) : t1)
429428
end
430429
next (t1 += 1)

spec/examples/schedule_spec.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -500,10 +500,10 @@
500500
occs.should == [t0]
501501
end
502502

503-
it 'should include long occurrences starting before and ending after' do
503+
it 'should not find occurrence with duration ending on start time' do
504504
t0 = Time.utc(2015, 10, 1, 12, 00)
505505
schedule = IceCube::Schedule.new(t0, :duration => IceCube::ONE_HOUR)
506-
schedule.occurs_between?(t0 + IceCube::ONE_HOUR, t0 + 2 * IceCube::ONE_HOUR, true).should be_true
506+
schedule.occurs_between?(t0 + IceCube::ONE_HOUR, t0 + 2 * IceCube::ONE_HOUR, true).should be_false
507507
end
508508

509509
it 'should quickly fetch a future time from a recurring schedule' do
@@ -518,6 +518,14 @@
518518
timing.should < 0.1
519519
occ.should == [t1]
520520
end
521+
522+
it 'should not include occurrence ending on start time' do
523+
t0 = Time.utc(2015, 10, 1, 10, 00)
524+
schedule = IceCube::Schedule.new(t0, :duration => IceCube::ONE_HOUR / 2)
525+
schedule.add_recurrence_rule IceCube::Rule.minutely(30).count(6)
526+
third_occ = schedule.next_occurrence(t0 + IceCube::ONE_HOUR, true)
527+
third_occ.should == t0 + IceCube::ONE_HOUR
528+
end
521529

522530
end
523531

@@ -527,8 +535,8 @@
527535
t0 = Time.utc(2013, 5, 18, 12, 34)
528536
schedule = IceCube::Schedule.new(t0)
529537
schedule.add_recurrence_rule IceCube::Rule.daily
530-
previous = schedule.previous_occurrence(t0 + 2 * ONE_DAY)
531-
previous.should == t0 + ONE_DAY
538+
previous = schedule.previous_occurrence(t0 + 2 * IceCube::ONE_DAY)
539+
previous.should == t0 + IceCube::ONE_DAY
532540
end
533541

534542
it 'returns nil given the start time' do
@@ -555,16 +563,16 @@
555563
t0 = Time.utc(2013, 5, 18, 12, 34)
556564
schedule = IceCube::Schedule.new(t0)
557565
schedule.add_recurrence_rule IceCube::Rule.daily
558-
previous = schedule.previous_occurrences(2, t0 + 3 * ONE_DAY)
559-
previous.should == [t0 + ONE_DAY, t0 + 2 * ONE_DAY]
566+
previous = schedule.previous_occurrences(2, t0 + 3 * IceCube::ONE_DAY)
567+
previous.should == [t0 + IceCube::ONE_DAY, t0 + 2 * IceCube::ONE_DAY]
560568
end
561569

562570
it 'limits the returned occurrences to a given count' do
563571
t0 = Time.utc(2013, 5, 18, 12, 34)
564572
schedule = IceCube::Schedule.new(t0)
565573
schedule.add_recurrence_rule IceCube::Rule.daily
566-
previous = schedule.previous_occurrences(999, t0 + 2 * ONE_DAY)
567-
previous.should == [t0, t0 + ONE_DAY]
574+
previous = schedule.previous_occurrences(999, t0 + 2 * IceCube::ONE_DAY)
575+
previous.should == [t0, t0 + IceCube::ONE_DAY]
568576
end
569577

570578
it 'returns empty array given the start time' do
@@ -601,7 +609,7 @@
601609
t1 = Time.utc(2013, 5, 31, 12, 34)
602610
schedule = IceCube::Schedule.new(t0)
603611
schedule.add_recurrence_rule IceCube::Rule.daily.until(t1 + 1)
604-
schedule.last(2).should == [t1 - ONE_DAY, t1]
612+
schedule.last(2).should == [t1 - IceCube::ONE_DAY, t1]
605613
end
606614

607615
it 'raises an error for a non-terminating schedule' do

0 commit comments

Comments
 (0)