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

use new headway periods #839

Merged
merged 1 commit into from
Nov 1, 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
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
Loading