Skip to content

Commit

Permalink
use timezoneless time for weekly rule realigns
Browse files Browse the repository at this point in the history
  • Loading branch information
larskuhnt committed Apr 25, 2024
1 parent 9952238 commit 30a2eb3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/ice_cube/rules/weekly_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ 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_time
super step_time, time.to_timezoneless_time
end

# Calculate how many days to the first wday validation in the correct
Expand Down
16 changes: 16 additions & 0 deletions lib/ice_cube/time_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,22 @@ def to_time
TimeUtil.build_in_zone(parts, @base)
end

# This is used keep the correct hour within the interval during DST
# changes. It will use the time from the schedule start time to lock the
# hour.
def to_timezoneless_time
unwrapped_time = to_time
Time.new(
unwrapped_time.year,
unwrapped_time.month,
unwrapped_time.day,
@time.hour,
@time.min,
@time.sec,
unwrapped_time.utc_offset
)
end

# DST-safely add an interval of time to the wrapped time
def add(type, val)
type = :day if type == :wday
Expand Down
2 changes: 1 addition & 1 deletion spec/examples/schedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@
ref_time = Time.utc(2024, 4, 24, 12, 0, 0)
occurrences = schedule.next_occurrences(3, ref_time)
expect(occurrences.map(&:iso8601)).to eq([
"2024-04-26T01:20:00+03:00",
"2024-04-26T00:20:00+03:00",
"2024-05-03T00:20:00+03:00",
"2024-05-10T00:20:00+03:00"
])
Expand Down
4 changes: 2 additions & 2 deletions spec/examples/weekly_rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ module IceCube
end

it "calculates the correct time from 2024-04-24 12:00:00 UTC" do
expect(rule.next_time(utc_tz.parse("2024-04-24 12:00:00"), start_time, nil).iso8601).to eq("2024-04-26T01:20:00+03:00")
expect(rule.next_time(utc_tz.parse("2024-04-24 12:00:00"), start_time, nil).iso8601).to eq("2024-04-26T00:20:00+03:00")
end

it "calculates the correct time from 2024-04-26 00:20:01 Africa/Cairo" do
Expand All @@ -422,7 +422,7 @@ module IceCube
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-26T01:20:00+03:00")
expect(subject.iso8601).to eq("2024-04-26T00:20:00+03:00")
end
end
end
Expand Down

0 comments on commit 30a2eb3

Please sign in to comment.