1
1
mirror of https://github.com/github/semantic.git synced 2024-12-20 21:31:48 +03:00

Refine our Duration api

This commit is contained in:
Timothy Clem 2018-09-14 13:50:02 -07:00
parent a6ce4f672f
commit d16c07dfc6

View File

@ -2,17 +2,33 @@ module Data.Duration
( Duration(..) ( Duration(..)
, fromSeconds , fromSeconds
, fromMilliseconds , fromMilliseconds
, fromMicroseconds
, fromNanoseconds
, toMicroseconds , toMicroseconds
) where ) where
-- A duration suitable for timeouts. -- A duration suitable for timeouts stored as an int of milliseconds.
newtype Duration = Milliseconds Int newtype Duration = Milliseconds Int
deriving (Eq, Ord)
fromMilliseconds :: Int -> Duration instance Show Duration where
fromMilliseconds n = Milliseconds (n * 1000) showsPrec _ (Milliseconds n) = shows n <> showString "ms"
fromSeconds :: Int -> Duration fromSeconds :: Int -> Duration
fromSeconds n = Milliseconds (n * 1000) fromSeconds n = fromMilliseconds (n * 1000)
-- milli = 10E-3 seconds
fromMilliseconds :: Int -> Duration
fromMilliseconds n | n <= 0 = Milliseconds 0
| otherwise = Milliseconds n
-- micro = 10E-6 seconds
fromMicroseconds :: Int -> Duration
fromMicroseconds n = fromMilliseconds (n `div` 1000)
-- nano = 10E-9 seconds
fromNanoseconds :: Int -> Duration
fromNanoseconds n = fromMicroseconds (n `div` 1000)
toMicroseconds :: Duration -> Int toMicroseconds :: Duration -> Int
toMicroseconds (Milliseconds n) = n * 1000 toMicroseconds (Milliseconds n) = n * 1000