summaryrefslogtreecommitdiff
path: root/src/System/ClockHelpers.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/System/ClockHelpers.hs')
-rw-r--r--src/System/ClockHelpers.hs32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/System/ClockHelpers.hs b/src/System/ClockHelpers.hs
new file mode 100644
index 0000000..244e221
--- /dev/null
+++ b/src/System/ClockHelpers.hs
@@ -0,0 +1,32 @@
+{-# OPTIONS_GHC -fno-warn-orphans #-}
+
+module System.ClockHelpers
+ ( Clock(..)
+ , TimeSpec(..)
+ , diffTimeSpec
+ , getTime
+ , showTimeSpec
+ , clockM
+ ) where
+
+import Control.Monad.IO.Class
+import Data.Aeson
+import Katip
+import System.Clock
+
+
+clockM :: (MonadIO m) => m a -> m (a, TimeSpec)
+clockM act = f <$> time <*> act <*> time
+ where
+ time = liftIO $ getTime Realtime
+ f start res end = (res, end `diffTimeSpec` start)
+
+showTimeSpec :: TimeSpec -> String
+showTimeSpec ts = show (fromIntegral (toNanoSecs ts) / (10**9) :: Double) ++ "s"
+
+-- To be able to use TimeSpec as part of katip contexts (katipAddContext)
+instance ToJSON TimeSpec
+instance ToObject TimeSpec
+instance LogItem TimeSpec where
+ payloadKeys v _ | v >= V1 = AllKeys
+ | otherwise = SomeKeys []