Shorter approximative duration (generally better for most usage)

This commit is contained in:
Yann Esposito (Yogsototh) 2018-12-28 20:18:45 +01:00
parent 1c0f8ff8d0
commit 1eb906c42c
Signed by untrusted user who does not match committer: yogsototh
GPG key ID: 7B19A4C650D59646
3 changed files with 35 additions and 2 deletions

View file

@ -1,5 +1,5 @@
name: human-readable-duration name: human-readable-duration
version: 0.2.0.1 version: 0.2.1.0
synopsis: Provide duration helper synopsis: Provide duration helper
homepage: http://github.com/yogsototh/human-readable-duration#readme homepage: http://github.com/yogsototh/human-readable-duration#readme
license: BSD3 license: BSD3

View file

@ -51,6 +51,39 @@ humanReadableDuration n
| n < year = let d = getDays n in if d > 0 then show d ++ " days " ++ humanReadableDuration (n `mod'` day) 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 "" | otherwise = let y = getYears n in if y > 0 then show y ++ " years " ++ humanReadableDuration (n `mod'` year) else ""
{- | `humanReadableDuration` take some time in micro-second precision and render a human readable duration.
>>> let duration = 2 * ms + 3 * oneSecond + 2 * minute + 33*day + 2*year
>>> duration
65923323.002000
>>> approximativeDuration duration
"2 years"
>>> let duration = 2 * ms + 3 * oneSecond + 2 * minute + 33*day
>>> approximativeDuration duration
"33 days"
>>> let duration = 2 * ms + 3 * oneSecond + 280 * minute
>>> approximativeDuration duration
"4 hours"
>>> let duration = 2 * ms + 3 * oneSecond + 22 * minute
>>> approximativeDuration duration
"22 min"
>>> let duration = 2 * ms + 3 * oneSecond
>>> approximativeDuration duration
"3s"
>>> let duration = 12 * ms
>>> approximativeDuration duration
"12ms"
-}
approximativeDuration :: Micro -> String
approximativeDuration n
| n < oneSecond = let mi = getMs n in show mi ++ "ms"
| n < minute = let s = getSeconds n in show s ++ "s"
| n < hour = let m = getMinutes n in show m ++ " min"
| n < day = let h = getHours n in show h ++ " hours"
| n < year = let d = getDays n in show d ++ " days"
| otherwise = let y = getYears n in show y ++ " years"
-- | Wrapper around any `Real` input, which works for `DiffTime` and -- | Wrapper around any `Real` input, which works for `DiffTime` and
-- `NominalDiffTime` from the time library, or a `Double` of seconds. -- `NominalDiffTime` from the time library, or a `Double` of seconds.
-- --

View file

@ -2,7 +2,7 @@ flags: {}
packages: packages:
- '.' - '.'
extra-deps: [] extra-deps: []
resolver: lts-5.15 resolver: lts-13.0
extra-lib-dirs: extra-lib-dirs:
- /usr/lib - /usr/lib
- /usr/local/lib - /usr/local/lib