Skip to content

Commit

Permalink
use new headway periods (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
panentheos authored Nov 1, 2024
1 parent 2b60188 commit ae1ab7d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
16 changes: 11 additions & 5 deletions lib/engine/config/headway.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Engine.Config.Headway do
defstruct @enforce_keys

@type headway_group :: String.t()
@type time_period :: :peak | :off_peak
@type time_period :: :peak | :off_peak | :saturday | :sunday
@type headway_id :: {headway_group(), time_period()}

@type t :: %__MODULE__{
Expand Down Expand Up @@ -33,19 +33,25 @@ defmodule Engine.Config.Headway do

@spec current_time_period(DateTime.t()) :: time_period()
def current_time_period(dt) do
day_of_week = dt |> DateTime.to_date() |> Date.day_of_week()

weekday? = day_of_week in 1..5
# Subtract 3 hours, since the service day ends at 3 AM
day_of_week = DateTime.add(dt, -3, :hour) |> Date.day_of_week()

rush_hour? =
(dt.hour >= 7 and dt.hour < 9) or (dt.hour >= 16 and dt.hour < 18) or
(dt.hour == 18 and dt.minute <= 30)

if(weekday? and rush_hour?, do: :peak, else: :off_peak)
case {day_of_week, rush_hour?} do
{6, _} -> :saturday
{7, _} -> :sunday
{_, true} -> :peak
{_, false} -> :off_peak
end
end

@spec parse_time_period(String.t()) :: {:ok, time_period()} | :error
defp parse_time_period("peak"), do: {:ok, :peak}
defp parse_time_period("off_peak"), do: {:ok, :off_peak}
defp parse_time_period("saturday"), do: {:ok, :saturday}
defp parse_time_period("sunday"), do: {:ok, :sunday}
defp parse_time_period(_), do: :error
end
34 changes: 10 additions & 24 deletions test/engine/config/headway_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,17 @@ defmodule Engine.Config.HeadwayTest do

describe "current_time_period/1" do
test "correctly determines peak and offpeak" do
dt1 = DateTime.from_naive!(~N[2020-03-20 06:00:00], "America/New_York")
dt2 = DateTime.from_naive!(~N[2020-03-20 07:00:00], "America/New_York")
dt3 = DateTime.from_naive!(~N[2020-03-20 07:45:00], "America/New_York")
dt4 = DateTime.from_naive!(~N[2020-03-20 09:00:01], "America/New_York")
dt5 = DateTime.from_naive!(~N[2020-03-20 12:00:00], "America/New_York")
dt6 = DateTime.from_naive!(~N[2020-03-20 15:00:00], "America/New_York")
dt7 = DateTime.from_naive!(~N[2020-03-20 16:00:00], "America/New_York")
dt8 = DateTime.from_naive!(~N[2020-03-20 18:00:00], "America/New_York")
dt9 = DateTime.from_naive!(~N[2020-03-20 18:20:00], "America/New_York")
dt10 = DateTime.from_naive!(~N[2020-03-20 18:40:00], "America/New_York")
dt11 = DateTime.from_naive!(~N[2020-03-21 06:00:00], "America/New_York")
dt12 = DateTime.from_naive!(~N[2020-03-21 08:00:00], "America/New_York")
assert DateTime.new!(~D[2020-03-20], ~T[18:00:00], "America/New_York")
|> Headway.current_time_period() == :peak

assert Headway.current_time_period(dt1) == :off_peak
assert Headway.current_time_period(dt2) == :peak
assert Headway.current_time_period(dt3) == :peak
assert Headway.current_time_period(dt4) == :off_peak
assert Headway.current_time_period(dt5) == :off_peak
assert Headway.current_time_period(dt6) == :off_peak
assert Headway.current_time_period(dt7) == :peak
assert Headway.current_time_period(dt8) == :peak
assert Headway.current_time_period(dt9) == :peak
assert Headway.current_time_period(dt10) == :off_peak
assert Headway.current_time_period(dt11) == :off_peak
assert Headway.current_time_period(dt12) == :off_peak
assert DateTime.new!(~D[2020-03-21], ~T[02:00:00], "America/New_York")
|> Headway.current_time_period() == :off_peak

assert DateTime.new!(~D[2020-03-21], ~T[08:00:00], "America/New_York")
|> Headway.current_time_period() == :saturday

assert DateTime.new!(~D[2020-03-22], ~T[12:00:00], "America/New_York")
|> Headway.current_time_period() == :sunday
end
end
end

0 comments on commit ae1ab7d

Please sign in to comment.