Why not just do , <lots of spaces> do: with no end? Personally I opt for putting ,do: as I like the alignment better.
Honestly I’d actually rather do this for the whole thing:
@seconds_in_one_year 31557600 # 31,557,600 seconds | Includes leap years.
@seconds_in_one_month 2629800 # 2,629,800 seconds | Includes leap years.
@seconds_in_one_week 604800 # 604,800 seconds | Does not include leap years.
@seconds_in_one_day 86400 # 86,400 seconds | Does not include leap years.
@seconds_in_one_hour 3600 # 3,600 seconds | Does not include leap years.
@seconds_in_one_minute 60 # 60 seconds | Does not include leap years.
@doc ~S"""
Take a number of seconds and return a string describing the length of time
in the most appropriate way. If we're dealing with seconds on the order of
hours, then return hours. If we're dealing with seconds on the order of
years, then return years. You get the idea.
This function takes leap years into account by treating one year as one
year plus one forth of a day.
"""
defp display_seconds(seconds), do: cond do
seconds > (3 * @seconds_in_one_year) -> {@seconds_in_one_year, "years"}
seconds > (3 * @seconds_in_one_month) -> {@seconds_in_one_month, "months"}
seconds > (3 * @seconds_in_one_week) -> {@seconds_in_one_week, "weeks"}
seconds > (3 * @seconds_in_one_day) -> {@seconds_in_one_day, "days"}
seconds > (3 * @seconds_in_one_hour) -> {@seconds_in_one_hour, "hours"}
seconds > (3 * @seconds_in_one_minute) -> {@seconds_in_one_minutes, "minutes"}
1 -> {1, "second"}
true -> {1, "seconds"}
end |> case do
{o, s} -> "#{Number.Delimit.number_to_delimited(seconds / o, precision: 0)} #{s}"
end
The formatter would of course destroy the formatting, so I’d break it up in to more functions, one that probably does the when guard checks and another to do the actual formatting via number_to_delimited, which is more readable anyway. ![]()






















