Skip to content

Commit

Permalink
Merge branch 'peek-travel:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Matsa59 authored Jan 3, 2024
2 parents ce777fe + f3ea473 commit 0337d59
Showing 1 changed file with 24 additions and 0 deletions.
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 @@ defmodule Cocktail.Validation.Shift do
time
|> shift_time("#{type}": amount)
|> apply_option(option)
|> maybe_dst_change(time)

{:change, new_time}
end
Expand All @@ -29,4 +30,27 @@ defmodule Cocktail.Validation.Shift do
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
_ -> shifted_time
end
end
end

0 comments on commit 0337d59

Please sign in to comment.