rjpj1998
rjpj1998

Reputation: 419

Converting a double to UTCTime

The title is all it is.

Assume I have a double:

let d = 11241241.10124012 :: Double

How do I get an instance of UTCTime corresponding to d, d as in unix seconds?

Upvotes: 0

Views: 462

Answers (1)

Simon Courtenage
Simon Courtenage

Reputation: 416

You need to convert the double to a fractional in order for it to be understood as a NominalDiffTime value (which is an instance of Fractional). Once it is understood as a NominalDiffTime value, then posixSecondsToUTCTime will handle the conversion (POSIXTime being a type synonym of NominalDiffTime).

import Data.Time.Clock
import Data.Time.Clock.POSIX(posixSecondsToUTCTime)

doubleToUTCTime d = posixSecondsToUTCTime $ realToFrac d

Upvotes: 1

Related Questions