Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] Weekly interval rules are sometimes calculated incorrectly #487

Merged
merged 11 commits into from
Oct 25, 2021
6 changes: 2 additions & 4 deletions lib/ice_cube/validated_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ def other_interval_validations
# to the given start time
def next_time(time, start_time, closing_time)
@time = time
unless @start_time
@start_time = realign(time, start_time)
@time = @start_time if @time < @start_time
end
@start_time ||= realign(time, start_time)
@time = @start_time if @time < @start_time

return nil unless find_acceptable_time_before(closing_time)

Expand Down
13 changes: 13 additions & 0 deletions spec/examples/weekly_rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,19 @@ module IceCube
expect(schedule.next_occurrence(sample[2] - 1)).to eq(sample[2])
end

it "should respect weekly intervals within narrow occurrence ranges" do
start_time = Time.utc(2020, 10, 27, 7, 0, 0)
schedule = Schedule.new(start_time, end_time: start_time + ONE_HOUR)
occurrence_start = Time.utc(2020, 11, 5, 0, 0, 0)
occurrence_end = Time.utc(2020, 11, 5, 23, 59, 59)

schedule.add_recurrence_rule IceCube::Rule.weekly(2).day(:thursday).hour_of_day(13)
schedule.add_recurrence_rule IceCube::Rule.weekly(1).day(:thursday).hour_of_day(12)
expect(schedule.occurrences_between(occurrence_start, occurrence_end)).to eq([
Time.utc(2020, 11, 5, 12, 0, 0)
])
end

it "should align next_occurrence with first valid weekday when schedule starts on a Wednesday" do
t0 = Time.utc(2017, 6, 7)
schedule = IceCube::Schedule.new(t0)
Expand Down