Skip to content

Commit

Permalink
Ressource GTFS : Localisation d'une durée (#4228)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitfred authored Sep 30, 2024
1 parent a758e12 commit ce2687b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<% is_gtfs_flex = not is_nil(@resource_history) and DB.ResourceHistory.gtfs_flex?(@resource_history)
associated_geojson = get_associated_geojson(@related_files)
associated_netex = get_associated_netex(@related_files) %>
associated_netex = get_associated_netex(@related_files)
locale = get_session(@conn, :locale) %>
<section>
<div class="grey-background">
<div class="container">
Expand Down Expand Up @@ -30,7 +31,7 @@ associated_netex = get_associated_netex(@related_files) %>
) %>
<%= unless is_nil(associated_geojson) or is_nil(associated_geojson.resource_history_last_up_to_date_at) do %>
<%= dgettext("validations", "This GeoJSON was up-to-date with the GTFS resource %{hours} ago.",
hours: hours_ago(associated_geojson.resource_history_last_up_to_date_at)
hours: hours_ago(associated_geojson.resource_history_last_up_to_date_at, locale)
) %>
<% end %>
</div>
Expand Down Expand Up @@ -69,7 +70,7 @@ associated_netex = get_associated_netex(@related_files) %>
<%= unless is_nil(associated_geojson.resource_history_last_up_to_date_at) do %>
<div class="is-centered no-margin">
<%= dgettext("validations", "Visualization up-to-date %{hours} ago.",
hours: hours_ago(associated_geojson.resource_history_last_up_to_date_at)
hours: hours_ago(associated_geojson.resource_history_last_up_to_date_at, locale)
) %>
</div>
<% end %>
Expand Down Expand Up @@ -118,7 +119,7 @@ associated_netex = get_associated_netex(@related_files) %>
date:
DateTimeDisplay.format_datetime_to_paris(
@validation.validation_timestamp,
get_session(@conn, :locale)
locale
),
validator_url: gtfs_validator_url()
)
Expand Down
35 changes: 19 additions & 16 deletions apps/transport/lib/transport_web/views/resource_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,36 @@ defmodule TransportWeb.ResourceView do

def max_display_errors, do: 50

def hours_ago(utcdatetime) do
DateTime.utc_now() |> DateTime.diff(utcdatetime) |> seconds_to_hours_minutes()
def hours_ago(utcdatetime, locale) do
DateTime.utc_now() |> DateTime.diff(utcdatetime) |> seconds_to_hours_minutes(locale)
end

@doc """
Converts seconds to a string showing hours and minutes.
Also work for negative input, even if not intended to use it that way.
iex> seconds_to_hours_minutes(3661)
"1 h 1 min"
iex> seconds_to_hours_minutes(60)
"1 min"
iex> seconds_to_hours_minutes(30)
"0 min"
iex> seconds_to_hours_minutes(-3661)
"-1 h 1 min"
iex> seconds_to_hours_minutes(3661, :en)
"1 hour and 1 minute"
iex> seconds_to_hours_minutes(60, :en)
"1 minute"
iex> seconds_to_hours_minutes(30, :en)
"0 minute"
iex> seconds_to_hours_minutes(-3661, :en)
"-1 hour and 1 minute"
"""
@spec seconds_to_hours_minutes(integer()) :: binary()
def seconds_to_hours_minutes(seconds) do
hours = div(seconds, 3600)
@spec seconds_to_hours_minutes(integer(), atom() | Cldr.LanguageTag.t()) :: binary()
def seconds_to_hours_minutes(seconds, locale \\ :en) do
duration = strip_seconds(seconds)

case hours do
0 -> "#{div(seconds, 60)} min"
hours -> "#{hours} h #{seconds |> rem(3600) |> div(60) |> abs()} min"
cond do
duration == 0 -> "0 minute"
duration < 0 -> "-#{Shared.DateTimeDisplay.format_duration(-duration, locale)}"
true -> Shared.DateTimeDisplay.format_duration(duration, locale)
end
end

def strip_seconds(seconds), do: div(seconds, 60) * 60

def download_availability_class(ratio) when ratio >= 0 and ratio <= 100 do
cond do
ratio == 100 -> "download_availability_100"
Expand Down

0 comments on commit ce2687b

Please sign in to comment.