Skip to content

Commit

Permalink
Update schedule.ex
Browse files Browse the repository at this point in the history
  • Loading branch information
brianberlin committed Sep 14, 2023
1 parent 988bd3d commit 92155c5
Show file tree
Hide file tree
Showing 13 changed files with 499 additions and 462 deletions.
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
elixir 1.12.1-otp-24
erlang 24.0.2
elixir 1.14.5-otp-25
erlang 25.3.2
26 changes: 11 additions & 15 deletions lib/cocktail/schedule.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ defmodule Cocktail.Schedule do
@spec new(Cocktail.time(), Cocktail.schedule_options()) :: t
def new(start_time, options \\ []) do
%__MODULE__{
start_time: no_ms(start_time),
start_time: start_time,
duration: options[:duration]
}
end

@doc false
@spec set_start_time(t, Cocktail.time()) :: t
def set_start_time(schedule, start_time), do: %{schedule | start_time: no_ms(start_time)}
def set_start_time(schedule, start_time), do: %{schedule | start_time: start_time}

@doc false
@spec set_duration(t, pos_integer) :: t
Expand Down Expand Up @@ -135,7 +135,7 @@ defmodule Cocktail.Schedule do
"""
@spec add_recurrence_time(t, Cocktail.time()) :: t
def add_recurrence_time(%__MODULE__{} = schedule, time),
do: %{schedule | recurrence_times: [no_ms(time) | schedule.recurrence_times]}
do: %{schedule | recurrence_times: [time | schedule.recurrence_times]}

@doc """
Adds an exception time to the schedule.
Expand All @@ -145,7 +145,7 @@ defmodule Cocktail.Schedule do
"""
@spec add_exception_time(t, Cocktail.time()) :: t
def add_exception_time(%__MODULE__{} = schedule, time),
do: %{schedule | exception_times: [no_ms(time) | schedule.exception_times]}
do: %{schedule | exception_times: [time | schedule.exception_times]}

@doc """
Creates a stream of occurrences from the given schedule.
Expand Down Expand Up @@ -184,21 +184,21 @@ defmodule Cocktail.Schedule do
...> schedule = start_time |> new() |> add_recurrence_rule(:daily)
...> schedule |> occurrences() |> Enum.take(3) |> Enum.map(&Timex.format!(&1, "{ISO:Extended}"))
["2017-01-02T10:00:00-08:00",
"2017-01-03T10:00:00-08:00",
"2017-01-04T10:00:00-08:00"]
"2017-01-03T10:00:00.000000-08:00",
"2017-01-04T10:00:00.000000-08:00"]
# using a NaiveDateTime with a duration
iex> start_time = ~N[2017-02-01 12:00:00]
iex> start_time = ~N[2017-02-01 12:00:00.000000]
...> schedule = start_time |> new(duration: 3_600) |> add_recurrence_rule(:weekly)
...> schedule |> occurrences() |> Enum.take(3)
[%Cocktail.Span{from: ~N[2017-02-01 12:00:00], until: ~N[2017-02-01 13:00:00]},
%Cocktail.Span{from: ~N[2017-02-08 12:00:00], until: ~N[2017-02-08 13:00:00]},
%Cocktail.Span{from: ~N[2017-02-15 12:00:00], until: ~N[2017-02-15 13:00:00]}]
[%Cocktail.Span{from: ~N[2017-02-01 12:00:00.000000], until: ~N[2017-02-01 13:00:00.000000]},
%Cocktail.Span{from: ~N[2017-02-08 12:00:00.000000], until: ~N[2017-02-08 13:00:00.000000]},
%Cocktail.Span{from: ~N[2017-02-15 12:00:00.000000], until: ~N[2017-02-15 13:00:00.000000]}]
"""
@spec occurrences(t, Cocktail.time() | nil) :: Enumerable.t()
def occurrences(%__MODULE__{} = schedule, start_time \\ nil) do
schedule
|> ScheduleState.new(no_ms(start_time))
|> ScheduleState.new(start_time)
|> Stream.unfold(&ScheduleState.next_time/1)
end

Expand Down Expand Up @@ -244,10 +244,6 @@ defmodule Cocktail.Schedule do
@spec to_string(t) :: String.t()
def to_string(%__MODULE__{} = schedule), do: Builder.String.build(schedule)

@spec no_ms(Cocktail.time() | nil) :: Cocktail.time() | nil
defp no_ms(nil), do: nil
defp no_ms(time), do: %{time | microsecond: {0, 0}}

defimpl Inspect, for: __MODULE__ do
import Inspect.Algebra

Expand Down
119 changes: 60 additions & 59 deletions test/cocktail/daily_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Cocktail.DailyTest do
alias Cocktail.Schedule

import Cocktail.TestSupport.DateTimeSigil
import Cocktail.TestSupport.Assertions

test "Daily" do
times =
Expand All @@ -13,11 +14,11 @@ defmodule Cocktail.DailyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(3)

assert times == [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-02 06:00:00 America/Los_Angeles],
~Y[2017-01-03 06:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-02 06:00:00 America/Los_Angeles],
~Y[2017-01-03 06:00:00 America/Los_Angeles]
])
end

test "Every 2 days" do
Expand All @@ -28,11 +29,11 @@ defmodule Cocktail.DailyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(3)

assert times == [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-03 06:00:00 America/Los_Angeles],
~Y[2017-01-05 06:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-03 06:00:00 America/Los_Angeles],
~Y[2017-01-05 06:00:00 America/Los_Angeles]
])
end

test "Every 2 days / Every 3 days" do
Expand All @@ -44,15 +45,15 @@ defmodule Cocktail.DailyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(7)

assert times == [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-03 06:00:00 America/Los_Angeles],
~Y[2017-01-04 06:00:00 America/Los_Angeles],
~Y[2017-01-05 06:00:00 America/Los_Angeles],
~Y[2017-01-07 06:00:00 America/Los_Angeles],
~Y[2017-01-09 06:00:00 America/Los_Angeles],
~Y[2017-01-10 06:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-01 06:00:00 America/Los_Angeles],
~Y[2017-01-03 06:00:00 America/Los_Angeles],
~Y[2017-01-04 06:00:00 America/Los_Angeles],
~Y[2017-01-05 06:00:00 America/Los_Angeles],
~Y[2017-01-07 06:00:00 America/Los_Angeles],
~Y[2017-01-09 06:00:00 America/Los_Angeles],
~Y[2017-01-10 06:00:00 America/Los_Angeles]
])
end

test "Daily; overridden start time" do
Expand All @@ -63,11 +64,11 @@ defmodule Cocktail.DailyTest do
|> Cocktail.Schedule.occurrences(~Y[2017-08-01 12:00:00 America/Los_Angeles])
|> Enum.take(3)

assert times == [
~Y[2017-08-02 06:00:00 America/Los_Angeles],
~Y[2017-08-03 06:00:00 America/Los_Angeles],
~Y[2017-08-04 06:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-08-02 06:00:00 America/Los_Angeles],
~Y[2017-08-03 06:00:00 America/Los_Angeles],
~Y[2017-08-04 06:00:00 America/Los_Angeles]
])
end

test "Daily on the 10th and 14th hours of the day" do
Expand All @@ -78,13 +79,13 @@ defmodule Cocktail.DailyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(5)

assert times == [
~Y[2017-01-01 10:00:00 America/Los_Angeles],
~Y[2017-01-01 14:00:00 America/Los_Angeles],
~Y[2017-01-02 10:00:00 America/Los_Angeles],
~Y[2017-01-02 14:00:00 America/Los_Angeles],
~Y[2017-01-03 10:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-01 10:00:00 America/Los_Angeles],
~Y[2017-01-01 14:00:00 America/Los_Angeles],
~Y[2017-01-02 10:00:00 America/Los_Angeles],
~Y[2017-01-02 14:00:00 America/Los_Angeles],
~Y[2017-01-03 10:00:00 America/Los_Angeles]
])
end

test "Every 6 days on Mondays and Fridays" do
Expand All @@ -95,16 +96,16 @@ defmodule Cocktail.DailyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(8)

assert times == [
~Y[2017-01-13 06:00:00 America/Los_Angeles],
~Y[2017-02-06 06:00:00 America/Los_Angeles],
~Y[2017-02-24 06:00:00 America/Los_Angeles],
~Y[2017-03-20 06:00:00 America/Los_Angeles],
~Y[2017-04-07 06:00:00 America/Los_Angeles],
~Y[2017-05-01 06:00:00 America/Los_Angeles],
~Y[2017-05-19 06:00:00 America/Los_Angeles],
~Y[2017-06-12 06:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-13 06:00:00 America/Los_Angeles],
~Y[2017-02-06 06:00:00 America/Los_Angeles],
~Y[2017-02-24 06:00:00 America/Los_Angeles],
~Y[2017-03-20 06:00:00 America/Los_Angeles],
~Y[2017-04-07 06:00:00 America/Los_Angeles],
~Y[2017-05-01 06:00:00 America/Los_Angeles],
~Y[2017-05-19 06:00:00 America/Los_Angeles],
~Y[2017-06-12 06:00:00 America/Los_Angeles]
])
end

test "Daily on Mondays and Fridays on the 10th and 14th hours of the day" do
Expand All @@ -115,13 +116,13 @@ defmodule Cocktail.DailyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(5)

assert times == [
~Y[2017-01-02 10:00:00 America/Los_Angeles],
~Y[2017-01-02 14:00:00 America/Los_Angeles],
~Y[2017-01-06 10:00:00 America/Los_Angeles],
~Y[2017-01-06 14:00:00 America/Los_Angeles],
~Y[2017-01-09 10:00:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-02 10:00:00 America/Los_Angeles],
~Y[2017-01-02 14:00:00 America/Los_Angeles],
~Y[2017-01-06 10:00:00 America/Los_Angeles],
~Y[2017-01-06 14:00:00 America/Los_Angeles],
~Y[2017-01-09 10:00:00 America/Los_Angeles]
])
end

test "Daily on Mondays and Fridays on the 10th and 14th hours of the day on the 15th and 45th minutes of the hour on the 0th and 30th seconds of the minute" do
Expand All @@ -138,17 +139,17 @@ defmodule Cocktail.DailyTest do
|> Cocktail.Schedule.occurrences()
|> Enum.take(9)

assert times == [
~Y[2017-01-02 10:15:00 America/Los_Angeles],
~Y[2017-01-02 10:15:30 America/Los_Angeles],
~Y[2017-01-02 10:45:00 America/Los_Angeles],
~Y[2017-01-02 10:45:30 America/Los_Angeles],
~Y[2017-01-02 14:15:00 America/Los_Angeles],
~Y[2017-01-02 14:15:30 America/Los_Angeles],
~Y[2017-01-02 14:45:00 America/Los_Angeles],
~Y[2017-01-02 14:45:30 America/Los_Angeles],
~Y[2017-01-06 10:15:00 America/Los_Angeles]
]
assert_equivalent_times(times, [
~Y[2017-01-02 10:15:00 America/Los_Angeles],
~Y[2017-01-02 10:15:30 America/Los_Angeles],
~Y[2017-01-02 10:45:00 America/Los_Angeles],
~Y[2017-01-02 10:45:30 America/Los_Angeles],
~Y[2017-01-02 14:15:00 America/Los_Angeles],
~Y[2017-01-02 14:15:30 America/Los_Angeles],
~Y[2017-01-02 14:45:00 America/Los_Angeles],
~Y[2017-01-02 14:45:30 America/Los_Angeles],
~Y[2017-01-06 10:15:00 America/Los_Angeles]
])
end

test "generating occurrences when when add_recurrence_time is used" do
Expand Down
12 changes: 7 additions & 5 deletions test/cocktail/edge_cases_test.exs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
defmodule Cocktail.EdgeCasesTest do
use ExUnit.Case

import Cocktail.TestSupport.Assertions

alias Cocktail.Schedule

test "override start time to before the schedule's start time" do
schedule = ~N[2017-10-01 09:00:00] |> Schedule.new() |> Schedule.add_recurrence_rule(:daily)

times = schedule |> Schedule.occurrences(~N[2017-09-01 00:00:00]) |> Enum.take(3)

assert times == [
~N[2017-10-01 09:00:00],
~N[2017-10-02 09:00:00],
~N[2017-10-03 09:00:00]
]
assert_equivalent_times(times, [
~N[2017-10-01 09:00:00],
~N[2017-10-02 09:00:00],
~N[2017-10-03 09:00:00]
])
end
end
Loading

0 comments on commit 92155c5

Please sign in to comment.