{-| Module : Utils.Time Copyright : (c) UU, 2019 License : MIT Maintainer : Tom Smeding Stability : experimental Portability : POSIX, macOS, Windows -} module Utils.Time where import Data.Time.Clock.System hiding (getSystemTime) import qualified Data.Time.Clock.System as Sys -- | A timestamp in milliseconds. newtype TimeStamp = TimeStamp Int deriving (Show, Read, Eq, Ord) -- | The number of milliseconds stored in the timestamp newtype. timestampMS :: TimeStamp -> Int timestampMS (TimeStamp n) = n -- | Offset the timestamp by the given number of milliseconds. plusMS :: TimeStamp -> Int -> TimeStamp plusMS (TimeStamp n) m = TimeStamp (n + m) -- | The number of milliseconds that the first timestamp is later than the -- second. subtractMS :: TimeStamp -> TimeStamp -> Int subtractMS (TimeStamp n) (TimeStamp m) = n - m -- | Return the current system time as a timestamp. getSystemTime :: IO TimeStamp getSystemTime = TimeStamp . systemToMS <$> Sys.getSystemTime where systemToMS tm = let MkSystemTime s ns = truncateSystemTimeLeapSecond tm in fromIntegral s * 1000 + fromIntegral ns `div` 1000000