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

fix: missing occurrences when there is a dst transition #229

Merged
merged 3 commits into from
Jan 3, 2024
Merged
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
24 changes: 24 additions & 0 deletions lib/cocktail/validation/shift.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
time
|> shift_time("#{type}": amount)
|> apply_option(option)
|> maybe_dst_change(time)

{:change, new_time}
end
Expand All @@ -29,4 +30,27 @@
defp apply_option(time, :beginning_of_day), do: time |> beginning_of_day()
defp apply_option(time, :beginning_of_hour), do: %{time | minute: 0, second: 0, microsecond: {0, 0}}
defp apply_option(time, :beginning_of_minute), do: %{time | second: 0, microsecond: {0, 0}}

defp maybe_dst_change(%DateTime{} = new_time, %DateTime{} = time) do
dst_diff = new_time.std_offset - time.std_offset

case dst_diff do
0 ->
new_time

diff ->
maybe_shift_time(new_time, time, diff)
end
end

defp maybe_dst_change(new_time, _time), do: new_time

defp maybe_shift_time(new_time, time, dst_diff) do
shifted_time = shift_time(new_time, seconds: -dst_diff)

case DateTime.compare(shifted_time, time) do
:eq -> new_time

Check warning on line 52 in lib/cocktail/validation/shift.ex

View check run for this annotation

Codecov / codecov/patch

lib/cocktail/validation/shift.ex#L52

Added line #L52 was not covered by tests
_ -> shifted_time
end
end
end
30 changes: 30 additions & 0 deletions test/cocktail/daily_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,34 @@ defmodule Cocktail.DailyTest do
|> Cocktail.Schedule.occurrences(~N[2015-01-24 18:30:00])
|> Enum.take(100) == [~N[2015-01-24 18:30:00]]
end

test "generating occurrences with spring forward transition" do
schedule =
~Y[2022-03-12 18:00:00 America/Los_Angeles]
|> Schedule.new()
|> Schedule.add_recurrence_rule(:daily)

times = schedule |> Schedule.occurrences() |> Enum.take(3)

assert times == [
~Y[2022-03-12 18:00:00 America/Los_Angeles],
~Y[2022-03-13 18:00:00 America/Los_Angeles],
~Y[2022-03-14 18:00:00 America/Los_Angeles]
]
end

test "generating occurrences with fall back transition" do
schedule =
~Y[2022-11-05 18:00:00 America/Los_Angeles]
|> Schedule.new()
|> Schedule.add_recurrence_rule(:daily)

times = schedule |> Schedule.occurrences() |> Enum.take(3)

assert times == [
~Y[2022-11-05 18:00:00 America/Los_Angeles],
~Y[2022-11-06 18:00:00 America/Los_Angeles],
~Y[2022-11-07 18:00:00 America/Los_Angeles]
]
end
end
15 changes: 15 additions & 0 deletions test/cocktail/weekly_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,19 @@ defmodule Cocktail.WeeklyTest do
~Y[2017-01-09 10:00:00 America/Los_Angeles]
]
end

test "Weekly with dst transition" do
times =
~Y[2022-03-12 06:00:00 America/Los_Angeles]
|> Cocktail.schedule()
|> Schedule.add_recurrence_rule(:weekly)
|> Cocktail.Schedule.occurrences()
|> Enum.take(3)

assert times == [
~Y[2022-03-12 06:00:00 America/Los_Angeles],
~Y[2022-03-19 06:00:00 America/Los_Angeles],
~Y[2022-03-26 06:00:00 America/Los_Angeles]
]
end
end
Loading