docspell/modules/webapp/src/main/elm/Util/Duration.elm

67 lines
1.0 KiB
Elm
Raw Normal View History

module Util.Duration exposing (Duration, toHuman)
-- 486ms -> 12s -> 1:05 -> 59:45 -> 1:02:12
2019-12-29 20:55:12 +00:00
type alias Duration =
Int
toHuman : Duration -> String
toHuman dur =
fromMillis dur
2019-12-29 20:55:12 +00:00
-- implementation
2019-12-29 20:55:12 +00:00
fromMillis : Int -> String
fromMillis ms =
case ms // 1000 of
0 ->
2019-12-29 20:55:12 +00:00
String.fromInt ms ++ "ms"
n ->
fromSeconds n
2019-12-29 20:55:12 +00:00
fromSeconds : Int -> String
fromSeconds sec =
case sec // 60 of
0 ->
2019-12-29 20:55:12 +00:00
String.fromInt sec ++ "s"
n ->
let
2019-12-29 20:55:12 +00:00
s =
sec - (n * 60)
in
2019-12-29 20:55:12 +00:00
fromMinutes n ++ ":" ++ num s
2019-12-29 20:55:12 +00:00
fromMinutes : Int -> String
fromMinutes min =
case min // 60 of
0 ->
2019-12-29 20:55:12 +00:00
num min
n ->
let
2019-12-29 20:55:12 +00:00
m =
min - (n * 60)
in
2019-12-29 20:55:12 +00:00
num n ++ ":" ++ num m
2019-12-29 20:55:12 +00:00
num : Int -> String
num n =
String.fromInt n
2019-12-29 20:55:12 +00:00
|> (++)
(if n < 10 then
"0"
else
""
)