{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NumericUnderscores #-}
module Control.Monad.Class.MonadTime.SI
( MonadTime (..)
, MonadMonotonicTime (..)
, Time (..)
, diffTime
, addTime
, DiffTime
, UTCTime
, diffUTCTime
, addUTCTime
, NominalDiffTime
) where
import Control.Monad.Reader
import Control.DeepSeq (NFData (..))
import Control.Monad.Class.MonadTime ( MonadMonotonicTimeNSec,
MonadTime (..), NominalDiffTime, UTCTime, diffUTCTime,
addUTCTime)
import qualified Control.Monad.Class.MonadTime as MonadTime
import NoThunks.Class (NoThunks (..))
import Data.Word (Word64)
import Data.Time.Clock (DiffTime)
import qualified Data.Time.Clock as Time
import GHC.Generics (Generic (..))
newtype Time = Time DiffTime
deriving stock (Time -> Time -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Time -> Time -> Bool
$c/= :: Time -> Time -> Bool
== :: Time -> Time -> Bool
$c== :: Time -> Time -> Bool
Eq, Eq Time
Time -> Time -> Bool
Time -> Time -> Ordering
Time -> Time -> Time
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Time -> Time -> Time
$cmin :: Time -> Time -> Time
max :: Time -> Time -> Time
$cmax :: Time -> Time -> Time
>= :: Time -> Time -> Bool
$c>= :: Time -> Time -> Bool
> :: Time -> Time -> Bool
$c> :: Time -> Time -> Bool
<= :: Time -> Time -> Bool
$c<= :: Time -> Time -> Bool
< :: Time -> Time -> Bool
$c< :: Time -> Time -> Bool
compare :: Time -> Time -> Ordering
$ccompare :: Time -> Time -> Ordering
Ord, Int -> Time -> ShowS
[Time] -> ShowS
Time -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Time] -> ShowS
$cshowList :: [Time] -> ShowS
show :: Time -> String
$cshow :: Time -> String
showsPrec :: Int -> Time -> ShowS
$cshowsPrec :: Int -> Time -> ShowS
Show, forall x. Rep Time x -> Time
forall x. Time -> Rep Time x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Time x -> Time
$cfrom :: forall x. Time -> Rep Time x
Generic)
deriving newtype Time -> ()
forall a. (a -> ()) -> NFData a
rnf :: Time -> ()
$crnf :: Time -> ()
NFData
deriving anyclass Context -> Time -> IO (Maybe ThunkInfo)
Proxy Time -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy Time -> String
$cshowTypeOf :: Proxy Time -> String
wNoThunks :: Context -> Time -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> Time -> IO (Maybe ThunkInfo)
noThunks :: Context -> Time -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> Time -> IO (Maybe ThunkInfo)
NoThunks
diffTime :: Time -> Time -> DiffTime
diffTime :: Time -> Time -> DiffTime
diffTime (Time DiffTime
t) (Time DiffTime
t') = DiffTime
t forall a. Num a => a -> a -> a
- DiffTime
t'
addTime :: DiffTime -> Time -> Time
addTime :: DiffTime -> Time -> Time
addTime DiffTime
d (Time DiffTime
t) = DiffTime -> Time
Time (DiffTime
d forall a. Num a => a -> a -> a
+ DiffTime
t)
infixr 9 `addTime`
class MonadMonotonicTimeNSec m => MonadMonotonicTime m where
getMonotonicTime :: m Time
default getMonotonicTime :: m Time
getMonotonicTime =
Word64 -> Time
conv forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). MonadMonotonicTimeNSec m => m Word64
MonadTime.getMonotonicTimeNSec
where
conv :: Word64 -> Time
conv :: Word64 -> Time
conv = DiffTime -> Time
Time forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> DiffTime
Time.picosecondsToDiffTime forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Num a => a -> a -> a
* Integer
1_000) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Integral a => a -> Integer
toInteger
instance MonadMonotonicTime IO where
instance MonadMonotonicTime m => MonadMonotonicTime (ReaderT r m) where
getMonotonicTime :: ReaderT r m Time
getMonotonicTime = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *). MonadMonotonicTime m => m Time
getMonotonicTime