Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
This module contains time unit data structures and functions to work with time.
Synopsis
- newtype Time (rat :: Rat) = Time {}
- type Second = 1 / 1
- type Millisecond = Second / 1000
- type Microsecond = Millisecond / 1000
- type Nanosecond = Microsecond / 1000
- type Picosecond = Nanosecond / 1000
- type Minute = 60 * Second
- type Hour = 60 * Minute
- type Day = 24 * Hour
- type Week = 7 * Day
- type Fortnight = 2 * Week
- type family UnitName (unit :: Rat) :: Symbol
- type KnownUnitName unit = KnownSymbol (UnitName unit)
- type KnownRatName unit = (KnownUnitName unit, KnownRat unit)
- unitNameVal :: forall (unit :: Rat). KnownUnitName unit => String
- time :: RatioNat -> Time unit
- floorUnit :: forall (unit :: Rat). Time unit -> Time unit
- floorRat :: forall b (unit :: Rat). Integral b => Time unit -> b
- ceilingUnit :: forall (unit :: Rat). Time unit -> Time unit
- ceilingRat :: forall b (unit :: Rat). Integral b => Time unit -> b
- toFractional :: forall r (unit :: Rat). Fractional r => Time unit -> r
- sec :: RatioNat -> Time Second
- ms :: RatioNat -> Time Millisecond
- mcs :: RatioNat -> Time Microsecond
- ns :: RatioNat -> Time Nanosecond
- ps :: RatioNat -> Time Picosecond
- minute :: RatioNat -> Time Minute
- hour :: RatioNat -> Time Hour
- day :: RatioNat -> Time Day
- week :: RatioNat -> Time Week
- fortnight :: RatioNat -> Time Fortnight
- toUnit :: forall (unitTo :: Rat) (unitFrom :: Rat). KnownDivRat unitFrom unitTo => Time unitFrom -> Time unitTo
- threadDelay :: forall (unit :: Rat) m. (KnownDivRat unit Microsecond, MonadIO m) => Time unit -> m ()
- getCPUTime :: forall (unit :: Rat) m. (KnownDivRat Picosecond unit, MonadIO m) => m (Time unit)
- timeout :: forall (unit :: Rat) m a. (MonadIO m, KnownDivRat unit Microsecond) => Time unit -> IO a -> m (Maybe a)
Time
newtype Time (rat :: Rat) Source #
Time unit is represented as type level rational multiplier with kind Rat
.
Instances
Typeable rat => Data (Time rat) Source # | |
Defined in Time.Units gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Time rat -> c (Time rat) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Time rat) # toConstr :: Time rat -> Constr # dataTypeOf :: Time rat -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Time rat)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Time rat)) # gmapT :: (forall b. Data b => b -> b) -> Time rat -> Time rat # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Time rat -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Time rat -> r # gmapQ :: (forall d. Data d => d -> u) -> Time rat -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Time rat -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Time rat -> m (Time rat) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Time rat -> m (Time rat) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Time rat -> m (Time rat) # | |
Monoid (Time rat) Source # | |
Semigroup (Time rat) Source # | Addition is associative binary operation for |
Enum (Time rat) Source # | |
Generic (Time rat) Source # | |
KnownUnitName unit => Read (Time unit) Source # | |
KnownUnitName unit => Show (Time unit) Source # | |
Eq (Time rat) Source # | |
Ord (Time rat) Source # | |
type Rep (Time rat) Source # | |
Defined in Time.Units |
Time data types
type Millisecond = Second / 1000 Source #
type Microsecond = Millisecond / 1000 Source #
type Nanosecond = Microsecond / 1000 Source #
type Picosecond = Nanosecond / 1000 Source #
type family UnitName (unit :: Rat) :: Symbol Source #
Type family for prettier show
of time units.
Instances
type UnitName (1 :% 1) Source # | |
Defined in Time.Units | |
type UnitName (1 :% 1000) Source # | |
Defined in Time.Units | |
type UnitName (1 :% 1000000) Source # | |
Defined in Time.Units | |
type UnitName (1 :% 1000000000) Source # | |
Defined in Time.Units | |
type UnitName (1 :% 1000000000000) Source # | |
Defined in Time.Units | |
type UnitName (60 :% 1) Source # | |
Defined in Time.Units | |
type UnitName (3600 :% 1) Source # | |
Defined in Time.Units | |
type UnitName (86400 :% 1) Source # | |
Defined in Time.Units | |
type UnitName (604800 :% 1) Source # | |
Defined in Time.Units | |
type UnitName (1209600 :% 1) Source # | |
Defined in Time.Units |
type KnownUnitName unit = KnownSymbol (UnitName unit) Source #
Constraint alias for KnownSymbol
UnitName
.
type KnownRatName unit = (KnownUnitName unit, KnownRat unit) Source #
Constraint alias for KnownUnitName
and KnownRat
for time unit.
unitNameVal :: forall (unit :: Rat). KnownUnitName unit => String Source #
Creation helpers
floorRat :: forall b (unit :: Rat). Integral b => Time unit -> b Source #
Returns the greatest integer not greater than given Time
.
ceilingRat :: forall b (unit :: Rat). Integral b => Time unit -> b Source #
Returns the smallest integer greater than or equal to the given Time
.
Since: 1.3.0
toFractional :: forall r (unit :: Rat). Fractional r => Time unit -> r Source #
Convert the Time
object to the Fractional
value.
Examples:
>>>
toFractional @Rational $ hour (1 % 8)
1 % 8
>>>
toFractional @Double $ hour (1 % 8)
0.125
Since: 1.3.0
ms :: RatioNat -> Time Millisecond Source #
Creates Millisecond
from given Natural
.
>>>
ms 42
42ms
mcs :: RatioNat -> Time Microsecond Source #
Creates Microsecond
from given Natural
.
>>>
mcs 42
42mcs
ns :: RatioNat -> Time Nanosecond Source #
Creates Nanosecond
from given Natural
.
>>>
ns 42
42ns
ps :: RatioNat -> Time Picosecond Source #
Creates Picosecond
from given Natural
.
>>>
ps 42
42ps
Functions
toUnit :: forall (unitTo :: Rat) (unitFrom :: Rat). KnownDivRat unitFrom unitTo => Time unitFrom -> Time unitTo Source #
Converts from one time unit to another time unit.
>>>
toUnit @Hour (minute 120)
2h
>>>
toUnit @Second (ms 7)
7/1000s
>>>
toUnit @Week (Time @Day 45)
6+3/7w
>>>
toUnit @Second @Minute (Time 3)
180s
>>>
toUnit (day 42000000) :: Time Second
3628800000000s
threadDelay :: forall (unit :: Rat) m. (KnownDivRat unit Microsecond, MonadIO m) => Time unit -> m () Source #
Convenient version of threadDelay
which takes
any time-unit and operates in any MonadIO.
>>> threadDelay $ sec 2 >>> threadDelay (2 :: Time Second) >>> threadDelay @Second 2
getCPUTime :: forall (unit :: Rat) m. (KnownDivRat Picosecond unit, MonadIO m) => m (Time unit) Source #
Similar to getCPUTime
but returns the CPU time used by the current
program in the given time unit.
The precision of this result is implementation-dependent.
>>> getCPUTime @Second 1064046949/1000000000s
:: forall (unit :: Rat) m a. (MonadIO m, KnownDivRat unit Microsecond) | |
=> Time unit | time |
-> IO a |
|
-> m (Maybe a) | returns |
Similar to timeout
but receiving any time unit
instead of number of microseconds.
>>> timeout (sec 1) (putStrLn "Hello O'Clock") Hello O'Clock Just ()
>>> timeout (ps 1) (putStrLn "Hello O'Clock") Nothing
>>> timeout (mcs 1) (putStrLn "Hello O'Clock") HellNothing