module Data.Duration
( humanReadableDuration
, humanReadableDuration'
, ms
, oneSecond
, minute
, hour
, day
, year
, getMs
, getSeconds
, getMinutes
, getHours
, getDays
, getYears
) where
import Data.Fixed (Fixed(..), Micro, div', mod')
humanReadableDuration :: Micro -> String
humanReadableDuration n
| n < oneSecond = let mi = getMs n in if mi > 0 then show mi ++ "ms" else ""
| n < minute = let s = getSeconds n in if s > 0 then show s ++ "s " ++ humanReadableDuration (n `mod'` oneSecond) else ""
| n < hour = let m = getMinutes n in if m > 0 then show m ++ " min " ++ humanReadableDuration (n `mod'` minute) else ""
| n < day = let h = getHours n in if h > 0 then show h ++ " hours " ++ humanReadableDuration (n `mod'` hour) else ""
| n < year = let d = getDays n in if d > 0 then show d ++ " days " ++ humanReadableDuration (n `mod'` day) else ""
| otherwise = let y = getYears n in if y > 0 then show y ++ " years " ++ humanReadableDuration (n `mod'` year) else ""
humanReadableDuration' :: Real a => a -> String
humanReadableDuration' = humanReadableDuration . realToFrac
ms :: Micro
ms = MkFixed 1000
oneSecond :: Micro
oneSecond = 1000 * ms
minute :: Micro
minute = 60 * oneSecond
hour :: Micro
hour = 60 * minute
day :: Micro
day = 24 * hour
year :: Micro
year = 365 * day
getMs :: Micro -> Integer
getMs n = n `div'` ms
getSeconds :: Micro -> Integer
getSeconds n = n `div'` oneSecond
getMinutes :: Micro -> Integer
getMinutes n = n `div'` minute
getHours :: Micro -> Integer
getHours n = n `div'` hour
getDays :: Micro -> Integer
getDays n = n `div'` day
getYears :: Micro -> Integer
getYears n = n `div'` year