Safe Haskell | None |
---|---|
Language | Haskell2010 |
Types for time intervals of various units. Each newtype wraps DiffTime
,
but they have different Num
instances. The intent is to use the record
selectors to write literals with particular units, like this:
>>>milliseconds
500 0.5s >>>hours
3 10800s >>>minutes
1.5 +seconds
30 120s
You can also go the other way using the constructors rather than the selectors:
>>>toRational
$
Minutes
(seconds
17) 17 % 60 >>>realToFrac
(Days
(hours
12)) ::Double
0.5
NOTE: the Real
and Fractional
instances just essentially add or strip the
unit label (as above), so you can't use realToFrac
to convert between the
units types here. Instead try convertDuration
which is less of a foot-gun.
The Read
instances for these types mirror the behavior of the RealFrac
instance wrt numeric literals for convenient serialization (e.g. when working
with env vars):
>>> read "1.2" :: Milliseconds Milliseconds {milliseconds = 0.0012s}
Generally, if you need to pass around a duration between functions you should
use DiffTime
directly. However if storing a duration in a type that will be
serialized, e.g. one having a ToJSON
instance, it is better to use one of
these explicit wrapper types so that it's obvious what units will be used.
Synopsis
- newtype Seconds = Seconds {}
- newtype Days = Days {}
- newtype Hours = Hours {}
- newtype Minutes = Minutes {}
- newtype Milliseconds = Milliseconds {}
- newtype Microseconds = Microseconds {}
- newtype Nanoseconds = Nanoseconds {}
- newtype TimeUnit (picosPerUnit :: Nat) = TimeUnit DiffTime
- type SecondsP n = n * 1000000000000
- natNum :: forall n a. (KnownNat n, Num a) => a
- class Duration d where
- fromDiffTime :: DiffTime -> d
- toDiffTime :: d -> DiffTime
- convertDuration :: (Duration x, Duration y) => x -> y
- diffTimeToMicroSeconds :: DiffTime -> Integer
Documentation
Instances
Eq Seconds Source # | |
Fractional Seconds Source # | |
Num Seconds Source # | |
Ord Seconds Source # | |
Read Seconds Source # | |
Real Seconds Source # | |
Defined in Data.Time.Clock.Units toRational :: Seconds -> Rational # | |
RealFrac Seconds Source # | |
Show Seconds Source # | |
Hashable Seconds Source # | |
Defined in Data.Time.Clock.Units | |
FromJSON Seconds Source # | |
Defined in Data.Time.Clock.Units parseJSON :: Value -> Parser Seconds parseJSONList :: Value -> Parser [Seconds] | |
ToJSON Seconds Source # | |
Defined in Data.Time.Clock.Units toEncoding :: Seconds -> Encoding toJSONList :: [Seconds] -> Value toEncodingList :: [Seconds] -> Encoding | |
Duration Seconds Source # | |
Defined in Data.Time.Clock.Units fromDiffTime :: DiffTime -> Seconds Source # toDiffTime :: Seconds -> DiffTime Source # | |
FromEnv Seconds Source # | |
Instances
Eq Days Source # | |
Fractional Days Source # | |
Num Days Source # | |
Ord Days Source # | |
Read Days Source # | |
Real Days Source # | |
Defined in Data.Time.Clock.Units toRational :: Days -> Rational # | |
RealFrac Days Source # | |
Show Days Source # | |
Hashable Days Source # | |
Defined in Data.Time.Clock.Units | |
Duration Days Source # | |
Defined in Data.Time.Clock.Units fromDiffTime :: DiffTime -> Days Source # toDiffTime :: Days -> DiffTime Source # |
Instances
Eq Hours Source # | |
Fractional Hours Source # | |
Num Hours Source # | |
Ord Hours Source # | |
Read Hours Source # | |
Real Hours Source # | |
Defined in Data.Time.Clock.Units toRational :: Hours -> Rational # | |
RealFrac Hours Source # | |
Show Hours Source # | |
Hashable Hours Source # | |
Defined in Data.Time.Clock.Units | |
Duration Hours Source # | |
Defined in Data.Time.Clock.Units fromDiffTime :: DiffTime -> Hours Source # toDiffTime :: Hours -> DiffTime Source # |
Instances
Eq Minutes Source # | |
Fractional Minutes Source # | |
Num Minutes Source # | |
Ord Minutes Source # | |
Read Minutes Source # | |
Real Minutes Source # | |
Defined in Data.Time.Clock.Units toRational :: Minutes -> Rational # | |
RealFrac Minutes Source # | |
Show Minutes Source # | |
Hashable Minutes Source # | |
Defined in Data.Time.Clock.Units | |
Duration Minutes Source # | |
Defined in Data.Time.Clock.Units fromDiffTime :: DiffTime -> Minutes Source # toDiffTime :: Minutes -> DiffTime Source # |
newtype Milliseconds Source #
Instances
newtype Microseconds Source #
Instances
newtype Nanoseconds Source #
Instances
class Duration d where Source #
Duration types isomorphic to DiffTime
, powering convertDuration
.
fromDiffTime :: DiffTime -> d Source #
toDiffTime :: d -> DiffTime Source #
Instances
convertDuration :: (Duration x, Duration y) => x -> y Source #
Safe conversion between duration units.