Merge pull request #1 from mstksg/master
Wrappers for conversions from duration types from the 'time' library
This commit is contained in:
commit
500b09fa24
2 changed files with 19 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
--------------------------------------------------------------------------------
|
||||
|
|
Loading…
Reference in a new issue