packages feed

human-readable-duration 0.1.1.0 → 0.2.0.0

raw patch · 2 files changed

+33/−45 lines, 2 filesdep −timePVP ok

version bump matches the API change (PVP)

Dependencies removed: time

API changes (from Hackage documentation)

- Data.Duration: humanReadableDiffTime :: DiffTime -> String
- Data.Duration: humanReadableNominalDiffTime :: NominalDiffTime -> String
+ Data.Duration: humanReadableDuration' :: Real a => a -> String
- Data.Duration: day :: Int
+ Data.Duration: day :: Micro
- Data.Duration: getDays :: Int -> Int
+ Data.Duration: getDays :: Micro -> Integer
- Data.Duration: getHours :: Int -> Int
+ Data.Duration: getHours :: Micro -> Integer
- Data.Duration: getMinutes :: Int -> Int
+ Data.Duration: getMinutes :: Micro -> Integer
- Data.Duration: getMs :: Int -> Int
+ Data.Duration: getMs :: Micro -> Integer
- Data.Duration: getSeconds :: Int -> Int
+ Data.Duration: getSeconds :: Micro -> Integer
- Data.Duration: getYears :: Int -> Int
+ Data.Duration: getYears :: Micro -> Integer
- Data.Duration: hour :: Int
+ Data.Duration: hour :: Micro
- Data.Duration: humanReadableDuration :: Int -> String
+ Data.Duration: humanReadableDuration :: Micro -> String
- Data.Duration: minute :: Int
+ Data.Duration: minute :: Micro
- Data.Duration: ms :: Int
+ Data.Duration: ms :: Micro
- Data.Duration: oneSecond :: Int
+ Data.Duration: oneSecond :: Micro
- Data.Duration: year :: Int
+ Data.Duration: year :: Micro

Files

human-readable-duration.cabal view
@@ -1,5 +1,5 @@ name:                human-readable-duration-version:             0.1.1.0+version:             0.2.0.0 synopsis:            Provide duration helper homepage:            http://github.com/yogsototh/human-readable-duration#readme license:             BSD3@@ -27,7 +27,6 @@   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
src/Data/Duration.hs view
@@ -1,7 +1,6 @@ module Data.Duration     ( humanReadableDuration-    , humanReadableDiffTime-    , humanReadableNominalDiffTime+    , humanReadableDuration'     -- durations     , ms     , oneSecond@@ -18,88 +17,78 @@     , getYears     ) where -import Data.Time.Clock (DiffTime, NominalDiffTime)+import Data.Fixed (Fixed(..), Micro, div', mod') --- | `humanReadableDuration` take some time in micro-seconds and render a human readable duration.+-- | `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 --    > humanReadableDuration duration --    > -- will return: "2 years 33 days 2 min 3s 32ms"-humanReadableDuration :: Int -> String+humanReadableDuration :: Micro -> String humanReadableDuration n   | n < oneSecond = let mi = getMs      n in if mi > 0 then show mi ++ "ms" else ""-  | n < minute = let s  = getSeconds n in if s  > 0 then show s  ++ "s " ++ humanReadableDuration (n `rem` oneSecond) else ""-  | n < hour   = let m  = getMinutes n in if m  > 0 then show m  ++ " min " ++ humanReadableDuration (n `rem` minute) else ""-  | n < day    = let h  = getHours   n in if h  > 0 then show h  ++ " hours " ++ humanReadableDuration (n `rem` hour) else ""-  | 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+  | n < minute = let s  = getSeconds n in if s  > 0 then show s  ++ "s " ++ humanReadableDuration (n `mod'` oneSecond) else ""+  | n < hour   = let m  = getMinutes n in if m  > 0 then show m  ++ " min " ++ humanReadableDuration (n `mod'` minute) else ""+  | n < day    = let h  = getHours   n in if h  > 0 then show h  ++ " hours " ++ humanReadableDuration (n `mod'` hour) 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 "" +-- | Wrapper around any `Real` input, which works for `DiffTime` and+-- `NominalDiffTime` from the time library, or a `Double` of seconds.+humanReadableDuration' :: Real a => a -> String+humanReadableDuration' = humanReadableDuration . realToFrac  -------------------------------------------------------------------------------- -- Durations --------------------------------------------------------------------------------  -- | number of micro seconds in one millisecond-ms :: Int-ms = 1000+ms :: Micro+ms = MkFixed 1000  -- | number of micro seconds in one second-oneSecond :: Int+oneSecond :: Micro oneSecond = 1000 * ms  -- | number of micro seconds in one minute-minute :: Int+minute :: Micro minute = 60 * oneSecond  -- | number of micro seconds in one hour-hour :: Int+hour :: Micro hour = 60 * minute  -- | number of micro seconds in one day-day :: Int+day :: Micro day = 24 * hour  -- | number of micro seconds in one year-year :: Int+year :: Micro year = 365 * day  -------------------------------------------------------------------------------- -- Retrieve some durations -------------------------------------------------------------------------------- -- | number of milli seconds given a duration in micro seconds-getMs :: Int -> Int-getMs n = n `div` ms+getMs :: Micro -> Integer+getMs n = n `div'` ms  -- | number of seconds given a duration in micro seconds-getSeconds :: Int -> Int-getSeconds n = n `div` oneSecond+getSeconds :: Micro -> Integer+getSeconds n = n `div'` oneSecond  -- | number of minutes given a duration in micro seconds-getMinutes :: Int -> Int-getMinutes n = n `div` minute+getMinutes :: Micro -> Integer+getMinutes n = n `div'` minute  -- | number of hours given a duration in micro seconds-getHours :: Int -> Int-getHours n = n `div` hour+getHours :: Micro -> Integer+getHours n = n `div'` hour  -- | number of days given a duration in micro seconds-getDays :: Int -> Int-getDays n = n `div` day+getDays :: Micro -> Integer+getDays n = n `div'` day  -- | number of years given a duration in micro seconds-getYears :: Int -> Int-getYears n = n `div` year+getYears :: Micro -> Integer+getYears n = n `div'` year