From 1bc30f8ebc052d241776425075db07c15f181359 Mon Sep 17 00:00:00 2001 From: Justin Le Date: Mon, 11 Jan 2016 23:45:25 -0800 Subject: [PATCH] Wrappers for conversions from duration types from the 'time' library --- human-readable-duration.cabal | 1 + src/Data/Duration.hs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/human-readable-duration.cabal b/human-readable-duration.cabal index ba8b41c..66a9d60 100644 --- a/human-readable-duration.cabal +++ b/human-readable-duration.cabal @@ -27,6 +27,7 @@ library hs-source-dirs: src exposed-modules: Data.Duration build-depends: base >= 4.7 && < 5 + , time >= 1.0 default-language: Haskell2010 -- test-suite human-readable-duration-test diff --git a/src/Data/Duration.hs b/src/Data/Duration.hs index 3607b54..f399a72 100644 --- a/src/Data/Duration.hs +++ b/src/Data/Duration.hs @@ -1,5 +1,7 @@ module Data.Duration ( humanReadableDuration + , humanReadableDiffTime + , humanReadableNominalDiffTime -- durations , ms , oneSecond @@ -16,6 +18,7 @@ module Data.Duration , getYears ) where +import Data.Time.Clock (DiffTime, NominalDiffTime) -- | `humanReadableDuration` take some time in micro-seconds and render a human readable duration. -- @@ -31,6 +34,21 @@ humanReadableDuration n | n < year = let d = getDays n in if d > 0 then show d ++ " days " ++ humanReadableDuration (n `rem` day) else "" | otherwise = let y = getYears n in if y > 0 then show y ++ " years " ++ humanReadableDuration (n `rem` year) else "" +-- | Give a human readable output of a `DiffTime` from the time library. +humanReadableDiffTime :: DiffTime -> String +humanReadableDiffTime = humanReadableDuration + . round + . (* (1e6 :: Double)) + . realToFrac + +-- | Give a human readable output of a `NominalDiffTime` from the time library. +humanReadableNominalDiffTime :: NominalDiffTime -> String +humanReadableNominalDiffTime = humanReadableDuration + . round + . (* (1e6 :: Double)) + . realToFrac + + -------------------------------------------------------------------------------- -- Durations --------------------------------------------------------------------------------