info and help with test

This commit is contained in:
Owen
2024-12-14 00:17:30 +01:00
parent 5975458407
commit 5d6e632a1d
10 changed files with 146 additions and 3 deletions
+19
View File
@@ -0,0 +1,19 @@
# https://rosettacode.org/wiki/Convert_seconds_to_compound_duration
defmodule PlantIdDiscordBot.Utils.Duration do
@minute 60
@hour @minute * 60
@day @hour * 24
@week @day * 7
@divisor [@week, @day, @hour, @minute, 1]
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