Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module contains time unit data structures and functions to work with time.
- newtype Time (rat :: Rat) = Time {}
- type Second = 1 :% 1
- type Millisecond = 1 :% 1000
- type Microsecond = 1 :% 1000000
- type Nanosecond = 1 :% 1000000000
- type Picosecond = 1 :% 1000000000000
- type Minute = 60 :% 1
- type Hour = 3600 :% 1
- type Day = 86400 :% 1
- type Week = 604800 :% 1
- type Fortnight = 1209600 :% 1
- type AllTimes = '[Fortnight, Week, Day, Hour, Minute, Second, Millisecond, Microsecond, Nanosecond, Picosecond]
- 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 :: Natural -> Time unit
- floorUnit :: forall (unit :: Rat). Time unit -> Time unit
- sec :: Natural -> Time Second
- ms :: Natural -> Time Millisecond
- mcs :: Natural -> Time Microsecond
- ns :: Natural -> Time Nanosecond
- ps :: Natural -> Time Picosecond
- minute :: Natural -> Time Minute
- hour :: Natural -> Time Hour
- day :: Natural -> Time Day
- week :: Natural -> Time Week
- fortnight :: Natural -> Time Fortnight
- (+:) :: forall (unitResult :: Rat) (unitLeft :: Rat). KnownDivRat unitLeft unitResult => Time unitLeft -> Time unitResult -> Time unitResult
- 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
.
Enum (Time rat) Source # | |
Eq (Time rat) Source # | |
Fractional (Time unit) Source # | Has the same behavior as derived instance, but |
Num (Time unit) Source # | Has the same behavior as derived instance, but |
Ord (Time rat) Source # | |
KnownUnitName unit => Read (Time unit) Source # | |
Real (Time rat) Source # | |
RealFrac (Time rat) Source # | |
KnownUnitName unit => Show (Time unit) Source # | |
Generic (Time rat) Source # | |
type Rep (Time rat) Source # | |
Time data types
type Millisecond = 1 :% 1000 Source #
type Microsecond = 1 :% 1000000 Source #
type Nanosecond = 1 :% 1000000000 Source #
type Picosecond = 1 :% 1000000000000 Source #
type AllTimes = '[Fortnight, Week, Day, Hour, Minute, Second, Millisecond, Microsecond, Nanosecond, Picosecond] Source #
Type-level list that consist of all times.
type family UnitName (unit :: Rat) :: Symbol Source #
Type family for prettier show
of time units.
type UnitName ((:%) 1 1) Source # | |
type UnitName ((:%) 1 1000) Source # | |
type UnitName ((:%) 1 1000000) Source # | |
type UnitName ((:%) 1 1000000000) Source # | |
type UnitName ((:%) 1 1000000000000) Source # | |
type UnitName ((:%) 60 1) Source # | |
type UnitName ((:%) 3600 1) Source # | |
type UnitName ((:%) 86400 1) Source # | |
type UnitName ((:%) 604800 1) Source # | |
type UnitName ((:%) 1209600 1) Source # | |
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
ms :: Natural -> Time Millisecond Source #
Creates Millisecond
from given Natural
.
>>>
ms 42
42ms :: Time Millisecond
mcs :: Natural -> Time Microsecond Source #
Creates Microsecond
from given Natural
.
>>>
mcs 42
42mcs :: Time Microsecond
ns :: Natural -> Time Nanosecond Source #
Creates Nanosecond
from given Natural
.
>>>
ns 42
42ns :: Time Nanosecond
ps :: Natural -> Time Picosecond Source #
Creates Picosecond
from given Natural
.
>>>
ps 42
42ps :: Time Picosecond
(+:) :: forall (unitResult :: Rat) (unitLeft :: Rat). KnownDivRat unitLeft unitResult => Time unitLeft -> Time unitResult -> Time unitResult Source #
Sums times of different units.
>>>
minute 1 +: sec 1
61s
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 (120 :: Time Minute)
2h
>>>
toUnit @Second (ms 7)
7/1000s
>>>
toUnit @Week (Time @Day 45)
45/7w
>>>
toUnit @Second @Minute 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). (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