Merge pull request #1 from mstksg/master

Wrappers for conversions from duration types from the 'time' library
This commit is contained in:
Yann Esposito 2016-01-12 09:48:06 +01:00
commit 500b09fa24
2 changed files with 19 additions and 0 deletions

View file

@ -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

View file

@ -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
--------------------------------------------------------------------------------