mirror of
https://github.com/TheRealOwenRees/plantid-discord-bot.git
synced 2026-07-22 20:16:57 +00:00
02372f2076
* added list of tasks to achieve * update deps * update elixir version in CI/CD pipeline * update todo with branch push checks * underscores separation of large numbers * update elixir version and deps * add basic module docs * remove IO.inspect call * check if list is empty rather than traversing list for length * revert empty list code * duration tests * add mime type tests * use logger instread of inspect, remove todos * replace list length check with Enum.empty * replace list length check with !Enum.empty? * numbers readability fix by using underscores on large numbers * iucn parser replaced case with multiclause function and added test * replace map and join with map_join * fix multiple spec issues where map() must be inside a list
30 lines
756 B
Elixir
30 lines
756 B
Elixir
defmodule PlantIdDiscordBot.Utils.Duration do
|
|
@moduledoc """
|
|
Duration conversion functions.
|
|
"""
|
|
@minute 60
|
|
@hour @minute * 60
|
|
@day @hour * 24
|
|
@week @day * 7
|
|
@divisor [@week, @day, @hour, @minute, 1]
|
|
|
|
@doc """
|
|
Convert seconds to a time string.
|
|
# https://rosettacode.org/wiki/Convert_seconds_to_compound_duration
|
|
|
|
## Examples
|
|
iex> PlantIdDiscordBot.Utils.Duration.sec_to_str(65)
|
|
iex> "1m 5s"
|
|
"""
|
|
def sec_to_str(sec) do
|
|
{_, [s, m, h, d, w]} =
|
|
Enum.reduce(@divisor, {sec, []}, fn divisor, {n, acc} ->
|
|
{rem(n, divisor), [div(n, divisor) | acc]}
|
|
end)
|
|
|
|
["#{w}w", "#{d}d", "#{h}h", "#{m}m", "#{s}s"]
|
|
|> Enum.reject(fn str -> String.starts_with?(str, "0") end)
|
|
|> Enum.join(" ")
|
|
end
|
|
end
|