From ae1ab7da1d2b667aa9956661db373cb0c2a3ebc8 Mon Sep 17 00:00:00 2001 From: Brett Heath-Wlaz Date: Fri, 1 Nov 2024 08:49:32 -0400 Subject: [PATCH] use new headway periods (#839) --- lib/engine/config/headway.ex | 16 +++++++++----- test/engine/config/headway_test.exs | 34 +++++++++-------------------- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/lib/engine/config/headway.ex b/lib/engine/config/headway.ex index 11c41b376..254f3074e 100644 --- a/lib/engine/config/headway.ex +++ b/lib/engine/config/headway.ex @@ -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__{ @@ -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 diff --git a/test/engine/config/headway_test.exs b/test/engine/config/headway_test.exs index 744a44ead..5076a2b8c 100644 --- a/test/engine/config/headway_test.exs +++ b/test/engine/config/headway_test.exs @@ -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