extra-1.7.16: Extra functions I use.
Safe HaskellSafe-Inferred
LanguageHaskell2010

System.Time.Extra

Description

Extra functions for working with times. Unlike the other modules in this package, there is no corresponding System.Time module. This module enhances the functionality from Data.Time.Clock, but in quite different ways.

Throughout, time is measured in Seconds, which is a type alias for Double.

Synopsis

Documentation

type Seconds = Double Source #

A type alias for seconds, which are stored as Double.

sleep :: Seconds -> IO () Source #

Sleep for a number of seconds.

fmap (round . fst) (duration $ sleep 1) == pure 1

timeout :: Seconds -> IO a -> IO (Maybe a) Source #

A version of timeout that takes Seconds and never overflows the bounds of an Int. In addition, the bug that negative timeouts run for ever has been fixed.

timeout (-3) (print 1) == pure Nothing
timeout 0.1  (print 1) == fmap Just (print 1)
do (t, _) <- duration $ timeout 0.1 $ sleep 1000; print t; pure $ t < 1
timeout 0.1  (sleep 2 >> print 1) == pure Nothing

showDuration :: Seconds -> String Source #

Show a number of seconds, typically a duration, in a suitable manner with reasonable precision for a human.

showDuration 3.435   == "3.44s"
showDuration 623.8   == "10m24s"
showDuration 62003.8 == "17h13m"
showDuration 1e8     == "27777h47m"

offsetTime :: IO (IO Seconds) Source #

Call once to start, then call repeatedly to get the elapsed time since the first call. The time is guaranteed to be monotonic. This function is robust to system time changes.

do f <- offsetTime; xs <- replicateM 10 f; pure $ xs == sort xs

offsetTimeIncrease :: IO (IO Seconds) Source #

Deprecated: Use offsetTime instead, which is guaranteed to always increase.

A synonym for offsetTime.

duration :: MonadIO m => m a -> m (Seconds, a) Source #

Record how long a computation takes in Seconds.

do (a,_) <- duration $ sleep 1; pure $ a >= 1 && a <= 1.5