Skip to content

Commit d49c4a4

Browse files
committed
Fixing speed issue on spans option
Unecessarily setting the start time search back to the start of the schedule for a spanned search, when in fact it simply needs to step back a duration.
1 parent 87a03e8 commit d49c4a4

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/ice_cube/schedule.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def enumerate_occurrences(opening_time, closing_time = nil, spans = false, &bloc
411411
opening_time = start_time if opening_time < start_time
412412
Enumerator.new do |yielder|
413413
reset
414-
t1 = full_required? || spans ? start_time : realign(opening_time)
414+
t1 = full_required? ? start_time : realign((spans ? opening_time - duration : opening_time))
415415
loop do
416416
break unless (t0 = next_time(t1, closing_time))
417417
break if closing_time && t0 > closing_time

spec/examples/schedule_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require File.dirname(__FILE__) + '/../spec_helper'
2+
require 'benchmark'
23

34
describe IceCube::Schedule do
45

@@ -504,6 +505,19 @@
504505
schedule = IceCube::Schedule.new(t0, :duration => IceCube::ONE_HOUR)
505506
schedule.occurs_between?(t0 + IceCube::ONE_HOUR, t0 + 2 * IceCube::ONE_HOUR, true).should be_true
506507
end
508+
509+
it 'should quickly fetch a future time from a recurring schedule' do
510+
t0 = Time.utc(2000, 10, 1, 00, 00)
511+
t1 = Time.utc(2015, 10, 1, 12, 00)
512+
schedule = IceCube::Schedule.new(t0, :duration => IceCube::ONE_HOUR - 1)
513+
schedule.add_recurrence_rule IceCube::Rule.hourly
514+
occ = nil
515+
timing = Benchmark.realtime do
516+
occ = schedule.remaining_occurrences_enumerator(t1, true).take(1)
517+
end
518+
timing.should < 0.1
519+
occ.should == [t1]
520+
end
507521

508522
end
509523

0 commit comments

Comments
 (0)