How do I convert DiffTime to NominalDiffTime?
Since NominalDiffTime
is an instance of Num
, you can create it with fromInteger
.
>>> fromInteger 1 :: NominalDiffTime
1s
NominalDiffTime
and DiffTime
both have RealFrac
instances and thus both Fractional
and Real
instances.
You can convert either of them to the other by
fromRational . toRational
Which has type (Real a, Fractional c) => a -> c
. This is a very common thing to do, and is provided in the standard prelude.
realToFrac :: (Real a, Fractional b) => a -> b
realToFrac = fromRational . toRational
DiffTime
and NominalDiffTime
both have Num
and Fractional
instances. This means you can use both integer and floating literals in place of either of them. All of the following work without any additional ceremony.
addUTCTime (-2)
addUTCTime 600
addUTCTime 0.5