github.com/simpleiot/simpleiot@v0.18.3/frontend/src/Utils/Duration.elm (about)

     1  module Utils.Duration exposing (Duration, toString)
     2  
     3  
     4  type alias Duration =
     5      Int
     6  
     7  
     8  msInSec : Int
     9  msInSec =
    10      1000
    11  
    12  
    13  msInMin : Int
    14  msInMin =
    15      msInSec * 60
    16  
    17  
    18  msInHour : Int
    19  msInHour =
    20      msInMin * 60
    21  
    22  
    23  msInDay : Int
    24  msInDay =
    25      msInHour * 24
    26  
    27  
    28  
    29  -- returns days and remainding ms
    30  
    31  
    32  toString : Duration -> String
    33  toString dur =
    34      let
    35          days =
    36              dur // msInDay
    37  
    38          daysR =
    39              remainderBy msInDay dur
    40  
    41          hours =
    42              daysR // msInHour
    43  
    44          hoursR =
    45              remainderBy msInHour daysR
    46  
    47          minutes =
    48              hoursR // msInMin
    49  
    50          minutesR =
    51              remainderBy msInMin hoursR
    52  
    53          seconds =
    54              minutesR // msInSec
    55      in
    56      (if days > 0 then
    57          String.fromInt days ++ "d "
    58  
    59       else
    60          ""
    61      )
    62          ++ (if hours > 0 then
    63                  String.fromInt hours ++ "h "
    64  
    65              else
    66                  ""
    67             )
    68          ++ (if minutes > 0 then
    69                  String.fromInt minutes ++ "m "
    70  
    71              else
    72                  ""
    73             )
    74          ++ (if seconds > 0 || (days <= 0 && hours <= 0 && minutes <= 0) then
    75                  String.fromInt seconds ++ "s"
    76  
    77              else
    78                  ""
    79             )