Skip to content

Commit fb6c657

Browse files
jakebrady5avit
authored andcommitted
Fix offset bug when day of start_time does not align with specified day rule (ice-cube-ruby#452)
This combination needs to apply the right offset for advancing to the first occurrence: - Weekly with frequency > 1 (biweekly, etc.) - with a weekday validation (e.g. Saturday) - declared as starting on a different weekday (e.g. Friday) - with an hour of day validation (even when declared to start on the expected hour)
1 parent 64b3951 commit fb6c657

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/ice_cube/validations/hour_of_day.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def realign(opening_time, start_time)
2323

2424
first_hour = Array(validations[:hour_of_day]).min_by(&:value)
2525
time = TimeUtil::TimeWrapper.new(start_time, false)
26-
if freq > 1
26+
if freq > 1 && base_interval_validation.type == :hour
2727
offset = first_hour.validate(opening_time, start_time)
2828
time.add(:hour, offset - freq)
2929
else

spec/examples/weekly_rule_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,26 @@ module IceCube
338338
end
339339
end
340340
end
341+
342+
# August 2018
343+
# Su Mo Tu We Th Fr Sa
344+
# 1 2 3 4
345+
# 5 6 7 8 9 10 11
346+
# 12 13 14 15 16 17 18
347+
# 19 20 21 22 23 24 25
348+
# 26 27 28 29 30 31
349+
context 'when day of start_time does not align with specified day rule' do
350+
let(:start_time) { Time.utc(2018, 8, 7, 10, 0, 0) }
351+
let(:end_time) { Time.utc(2018, 8, 7, 15, 0, 0) }
352+
let(:biweekly) { IceCube::Rule.weekly(2).day(:saturday).hour_of_day(10) }
353+
let(:schedule) { IceCube::Schedule.new(start_time, end_time: end_time) { |s| s.rrule biweekly } }
354+
let(:range) { [Time.utc(2018, 8, 11, 7, 0, 0), Time.utc(2018, 8, 12, 6, 59, 59)] }
355+
let(:expected_date) { Time.utc(2018, 8, 11, 10, 0, 0) }
356+
357+
it 'should align to the correct day with the spans option' do
358+
expect(schedule.occurrences_between(range.first, range.last, spans: true)).to include expected_date
359+
end
360+
end
341361
end
342362

343363
describe "using occurs_between with a weekly schedule" do

0 commit comments

Comments
 (0)