From 4bde6d7dd1cb1e5be41c9c7ddacccd05cc1cf840 Mon Sep 17 00:00:00 2001 From: Josh Larson Date: Thu, 23 Jan 2025 11:19:04 -0500 Subject: [PATCH] chore: Add `&Util.kitchen_downcase_time/1` (#2337) --- .../trip_planner/itinerary_summary.ex | 6 +----- .../components/trip_planner/place.ex | 8 +------ .../components/trip_planner/results.ex | 8 +------ .../trip_planner/results_summary.ex | 2 +- lib/util/util.ex | 21 +++++++++++++++++++ 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/lib/dotcom_web/components/trip_planner/itinerary_summary.ex b/lib/dotcom_web/components/trip_planner/itinerary_summary.ex index 72a22af406..6823aa447b 100644 --- a/lib/dotcom_web/components/trip_planner/itinerary_summary.ex +++ b/lib/dotcom_web/components/trip_planner/itinerary_summary.ex @@ -12,7 +12,7 @@ defmodule DotcomWeb.Components.TripPlanner.ItinerarySummary do
- {format_datetime_full(@summary.start)} - {format_datetime_full(@summary.stop)} + {Util.kitchen_downcase_time(@summary.start)} - {Util.kitchen_downcase_time(@summary.stop)}
{@summary.duration} min @@ -130,8 +130,4 @@ defmodule DotcomWeb.Components.TripPlanner.ItinerarySummary do defp zindex(index) do "z-#{50 - index * 10}" end - - defp format_datetime_full(datetime) do - Timex.format!(datetime, "%-I:%M%p", :strftime) |> String.downcase() - end end diff --git a/lib/dotcom_web/components/trip_planner/place.ex b/lib/dotcom_web/components/trip_planner/place.ex index c50f966838..37ea2acc64 100644 --- a/lib/dotcom_web/components/trip_planner/place.ex +++ b/lib/dotcom_web/components/trip_planner/place.ex @@ -31,7 +31,7 @@ defmodule DotcomWeb.Components.TripPlanner.Place do - +
""" end @@ -53,10 +53,4 @@ defmodule DotcomWeb.Components.TripPlanner.Place do """ end - - defp format_time(datetime) do - datetime - |> Timex.format!("%-I:%M%p", :strftime) - |> String.downcase() - end end diff --git a/lib/dotcom_web/components/trip_planner/results.ex b/lib/dotcom_web/components/trip_planner/results.ex index b2a346a58c..cea62d3f3f 100644 --- a/lib/dotcom_web/components/trip_planner/results.ex +++ b/lib/dotcom_web/components/trip_planner/results.ex @@ -122,7 +122,7 @@ defmodule DotcomWeb.Components.TripPlanner.Results do phx-click="select_itinerary" phx-value-index={index} > - {formatted_time(time)} + {Util.kitchen_downcase_time(time)}
@@ -130,10 +130,4 @@ defmodule DotcomWeb.Components.TripPlanner.Results do """ end - - defp formatted_time(time) do - time - |> Timex.format!("%-I:%M%p", :strftime) - |> String.downcase() - end end diff --git a/lib/dotcom_web/components/trip_planner/results_summary.ex b/lib/dotcom_web/components/trip_planner/results_summary.ex index 00efcccff8..492b77f8f3 100644 --- a/lib/dotcom_web/components/trip_planner/results_summary.ex +++ b/lib/dotcom_web/components/trip_planner/results_summary.ex @@ -65,7 +65,7 @@ defmodule DotcomWeb.Components.TripPlanner.ResultsSummary do preamble = if Map.get(params, :datetime_type) == "arrive_by", do: "Arriving by ", else: "Leaving at " - time_description = Timex.format!(datetime, "{h12}:{m}{am}") + time_description = Util.kitchen_downcase_time(datetime) date_description = Timex.format!(datetime, "{WDfull}, {Mfull} ") preamble <> time_description <> " on " <> date_description <> Inflex.ordinalize(datetime.day) end diff --git a/lib/util/util.ex b/lib/util/util.ex index 163f0cccd8..b2ca84469e 100644 --- a/lib/util/util.ex +++ b/lib/util/util.ex @@ -204,6 +204,27 @@ defmodule Util do def local_tz, do: @local_tz + @doc """ + + Provides a user-friendly display of time based on the "kitchen" + format, but with am/pm instead of AM/PM. + + ## Examples + iex> Util.kitchen_downcase_time(~T[08:30:00]) + "8:30am" + + iex> Util.kitchen_downcase_time(~T[20:30:00]) + "8:30pm" + + # Works for DateTime and NaiveDateTime inputs as well + iex> Util.kitchen_downcase_time(~N[2018-01-17T20:30:00]) + "8:30pm" + """ + @spec kitchen_downcase_time(DateTime.t() | NaiveDateTime.t() | Time.t()) :: String.t() + def kitchen_downcase_time(time) do + time |> Timex.format!("{kitchen}") |> String.downcase() + end + @doc """ Converts an `{:error, _}` tuple to a default value.