Skip to content

Commit

Permalink
chore: Add &Util.kitchen_downcase_time/1 (#2337)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlarson authored Jan 23, 2025
1 parent 69dbd37 commit 4bde6d7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
6 changes: 1 addition & 5 deletions lib/dotcom_web/components/trip_planner/itinerary_summary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule DotcomWeb.Components.TripPlanner.ItinerarySummary do
<div>
<div class="flex flex-row mb-3 font-bold text-lg justify-between">
<div>
{format_datetime_full(@summary.start)} - {format_datetime_full(@summary.stop)}
{Util.kitchen_downcase_time(@summary.start)} - {Util.kitchen_downcase_time(@summary.stop)}
</div>
<div>
{@summary.duration} min
Expand Down Expand Up @@ -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
8 changes: 1 addition & 7 deletions lib/dotcom_web/components/trip_planner/place.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule DotcomWeb.Components.TripPlanner.Place do
</strong>
</.wrap_with_url>
<time class="ml-auto text-right text-sm text-nowrap">{format_time(@time)}</time>
<time class="ml-auto text-right text-sm text-nowrap">{Util.kitchen_downcase_time(@time)}</time>
</div>
"""
end
Expand All @@ -53,10 +53,4 @@ defmodule DotcomWeb.Components.TripPlanner.Place do
</a>
"""
end

defp format_time(datetime) do
datetime
|> Timex.format!("%-I:%M%p", :strftime)
|> String.downcase()
end
end
8 changes: 1 addition & 7 deletions lib/dotcom_web/components/trip_planner/results.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,12 @@ defmodule DotcomWeb.Components.TripPlanner.Results do
phx-click="select_itinerary"
phx-value-index={index}
>
{formatted_time(time)}
{Util.kitchen_downcase_time(time)}
</.button>
</div>
</div>
<.itinerary_detail itinerary={@itinerary} />
</div>
"""
end

defp formatted_time(time) do
time
|> Timex.format!("%-I:%M%p", :strftime)
|> String.downcase()
end
end
2 changes: 1 addition & 1 deletion lib/dotcom_web/components/trip_planner/results_summary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions lib/util/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4bde6d7

Please sign in to comment.