extra-1.4.2: Extra functions I use.

Safe Haskell | Safe
Language | Haskell2010

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

- type Seconds = Double
- sleep :: Seconds -> IO ()
- timeout :: Seconds -> IO a -> IO (Maybe a)
- subtractTime :: UTCTime -> UTCTime -> Seconds
- showDuration :: Seconds -> String
- offsetTime :: IO (IO Seconds)
- offsetTimeIncrease :: IO (IO Seconds)
- duration :: IO a -> IO (Seconds, a)

# Documentation

type Seconds = Double

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

sleep :: Seconds -> IO ()

Sleep for a number of seconds.

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

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

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) == return Nothing
    timeout 0.1  (print 1) == fmap Just (print 1)
    timeout 0.1  (sleep 2 >> print 1) == return Nothing
    do (t, _) <- duration $ timeout 0.1 $ sleep 1000; return $ t < 1

subtractTime :: UTCTime -> UTCTime -> Seconds

Calculate the difference between two times in seconds. Usually the first time will be the end of an event, and the second time will be the beginning.

    \a b -> a > b ==> subtractTime a b > 0

showDuration :: Seconds -> String

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

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

offsetTime :: IO (IO Seconds)

Call once to start, then call repeatedly to get the elapsed time since the first call. Values will usually increase, unless the system clock is updated (if you need the guarantee, see `offsetTimeIncrease`).

offsetTimeIncrease :: IO (IO Seconds)

Like `offsetTime`, but results will never decrease (though they may stay the same).

    do f <- offsetTimeIncrease; xs <- replicateM 10 f; return $ xs == sort xs

duration :: IO a -> IO (Seconds, a)

Record how long a computation takes in `Seconds`.