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

Account for TZ offsets for event durations and next occurrence #499

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/ice_cube/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,12 @@ def next_time(time, closing_time)
end
break unless min_time
next (time = min_time + 1) if exception_time?(min_time)
break Occurrence.new(min_time, min_time + duration)

e_time = min_time + duration
# Make sure to add the timezone difference, in case we span tz zones (offsets)
# majority of cases this will be 0, but when we span tz zones, we need to account for it
e_time += TimeUtil.zone_offset_delta(min_time, e_time)
break Occurrence.new(min_time, e_time)
end
end

Expand Down
11 changes: 11 additions & 0 deletions lib/ice_cube/time_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,17 @@ def self.subsec(time)
end
end

# Returns the timezone delta (seconds) between 2 times.
# - For times in the same timezone this will return 0.
# - For times than span offsets (think Daylight Savings time to Standard Time)
# this number will be positive or negative, depening if you "spring foward" or "fall back".
# - The order of arguments is important, in 99% of use cases t0 is a date *before* t1 (aka time only moves foward).
# - For "fall back" this will be positive, aka we gained time (usually an hour, 3600)
# - For "spring foward" this will be negative, aka we lost time (usually an hour, -3600)
def self.zone_offset_delta(t0, t1)
t0.utc_offset - t1.utc_offset
end

# A utility class for safely moving time around
class TimeWrapper

Expand Down