summaryrefslogtreecommitdiff
path: root/src/System/ClockHelpers.hs
blob: 244e2210b1cc4cf817e124cfe362e1b7814ff55b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 []