From 1eb906c42cff1cccec98066876d682f443382f90 Mon Sep 17 00:00:00 2001 From: "Yann Esposito (Yogsototh)" Date: Fri, 28 Dec 2018 20:18:45 +0100 Subject: [PATCH] Shorter approximative duration (generally better for most usage) --- human-readable-duration.cabal | 2 +- src/Data/Duration.hs | 33 +++++++++++++++++++++++++++++++++ stack.yaml | 2 +- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/human-readable-duration.cabal b/human-readable-duration.cabal index d33fdb8..960142f 100644 --- a/human-readable-duration.cabal +++ b/human-readable-duration.cabal @@ -1,5 +1,5 @@ name: human-readable-duration -version: 0.2.0.1 +version: 0.2.1.0 synopsis: Provide duration helper homepage: http://github.com/yogsototh/human-readable-duration#readme license: BSD3 diff --git a/src/Data/Duration.hs b/src/Data/Duration.hs index a8d1459..1381ab2 100644 --- a/src/Data/Duration.hs +++ b/src/Data/Duration.hs @@ -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 "" | 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 -- `NominalDiffTime` from the time library, or a `Double` of seconds. -- diff --git a/stack.yaml b/stack.yaml index 05ac63b..cd893c4 100644 --- a/stack.yaml +++ b/stack.yaml @@ -2,7 +2,7 @@ flags: {} packages: - '.' extra-deps: [] -resolver: lts-5.15 +resolver: lts-13.0 extra-lib-dirs: - /usr/lib - /usr/local/lib