Skip to content

Commit

Permalink
review fixes and added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
larskuhnt committed Apr 26, 2024
1 parent b6bad31 commit 0df29bc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
9 changes: 8 additions & 1 deletion lib/ice_cube/rules/weekly_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ def realign(step_time, start_time)
time = TimeUtil::TimeWrapper.new(start_time)
offset = wday_offset(step_time, start_time)
time.add(:day, offset)
super step_time, time.to_timezoneless_time
realigned_time = time.to_time
# when the realigned time is in a different hour, we need to adjust the
# time to the correct hour with a fixed timezone offset, otherwise
# the time will be off by an hour
# WARNING: if the next DST change is within the interval, the occurrences
# after the next DST change will be off by an hour because the timezone is fixed
realigned_time = time.to_timezoneless_time if realigned_time.hour != start_time.hour
super step_time, realigned_time
end

# Calculate how many days to the first wday validation in the correct
Expand Down
2 changes: 1 addition & 1 deletion spec/examples/fixed_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
expect(validation.validate(time_in_zone, start_time)).to eq 1
end
end
end
end
24 changes: 16 additions & 8 deletions spec/examples/schedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -391,31 +391,39 @@
Time.utc(2014, 1, 2, 0o1, 34, 56)]
end

context "Cairo timezone" do
context "Cairo timezone", system_time_zone: 'Africa/Cairo' do
require "active_support/time"

let(:schedule) do
IceCube::Schedule.from_yaml("---\n:start_time:\n :time: 2022-05-05 22:20:00.000000000 Z\n :zone: Africa/Cairo\n:end_time:\n :time: 2022-05-06 21:40:00.000000000 Z\n :zone: Africa/Cairo\n:rrules:\n- :validations:\n :day:\n - 5\n :rule_type: IceCube::WeeklyRule\n :interval: 1\n :week_start: 1\n:rtimes: []\n:extimes: []\n")
# IceCube::Schedule.new(ActiveSupport::TimeZone['Africa/Cairo'].parse("2022-05-05 00:20:00")).tap do |schedule|
# schedule.add_recurrence_rule IceCube::Rule.weekly.day(:friday)
# end
end

it "has the correct start time" do
expect(schedule.start_time.iso8601).to eq("2022-05-06T00:20:00+02:00")
end

it "calculates the corret occurrences from 2024-04-24" do
ref_time = Time.utc(2024, 4, 24, 12, 0, 0)
occurrences = schedule.next_occurrences(3, ref_time)
it "has the correct start time timezone" do
expect(schedule.start_time.zone).to eq("EET")
end

it "calculates the correct occurrences from 2024-04-24" do
occurrences = schedule.next_occurrences(3, Time.utc(2024, 4, 24, 12, 0, 0))
expect(occurrences.map(&:iso8601)).to eq([
"2024-04-26T00:20:00+03:00",
"2024-05-03T00:20:00+03:00",
"2024-05-10T00:20:00+03:00"
"2024-05-10T00:20:00+03:00",
])
end

it "calculates the corret occurrences from 2024-04-21" do
occurrences = schedule.next_occurrences(3, Time.utc(2024, 4, 21, 12, 0, 0))
it "calculates the correct occurrences from 2024-04-17" do
occurrences = schedule.next_occurrences(3, Time.utc(2024, 4, 17, 12, 0, 0))
expect(occurrences.map(&:iso8601)).to eq([
"2024-04-19T00:20:00+02:00",
"2024-04-26T01:20:00+03:00",
"2024-05-03T00:20:00+03:00",
"2024-05-10T00:20:00+03:00"
])
end
end
Expand Down
28 changes: 23 additions & 5 deletions spec/examples/weekly_rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -411,19 +411,37 @@ module IceCube
end

describe :realign do
let(:cairo_tz) { ActiveSupport::TimeZone["Africa/Cairo"] }
require "active_support/time"

let(:timezone_name) { "Africa/Cairo" }
let(:timezone) { ActiveSupport::TimeZone[timezone_name] }
let(:utc_tz) { ActiveSupport::TimeZone["UTC"] }
let(:start_time) { cairo_tz.parse("2022-05-22 00:20:00") }
let(:start_time) { timezone.parse("2022-05-22 00:20:00") }
let(:time) { utc_tz.parse("2024-04-24 12:00:00") }
let(:rule) { Rule.weekly(1, :monday).day(:friday) }
let(:recurrence_day) { :friday }
let(:rule) { Rule.weekly(1, :monday).day(recurrence_day) }

subject { rule.realign(time, start_time) }

it { puts cairo_tz.parse("2024-04-26T00:20:00") }

it "realigns the start time to the correct time" do
expect(subject.iso8601).to eq("2024-04-26T00:20:00+03:00")
end

context "Berlin timezone" do
let(:recurrence_day) { :sunday }
let(:timezone_name) { "Europe/Berlin" }
let(:start_time) { timezone.parse("2024-03-24 02:30:00") }
let(:time) { timezone.parse("2024-03-27 02:30:00") }

# the next occurrence is on sunday within the DST shift
# where the clock is set forward from 02:00 to 03:00
# so the next occurrence is on actually on 03:30 but this
# would result in faulty start times for the following
# occurrences (03:30 instead of 02:30)
it "realigns the start time to the correct time" do
expect(subject.iso8601).to eq("2024-03-31T02:30:00+02:00")
end
end
end
end
end

0 comments on commit 0df29bc

Please sign in to comment.