Chris Martin
Chris Martin

Reputation: 30756

How do you get a millisecond-precision unix timestamp in Haskell?

What is the Haskell equivalent of this shell command?

$ date "+%s%3N"
1489694389603

Upvotes: 11

Views: 4060

Answers (1)

Chris Martin
Chris Martin

Reputation: 30756

Using the time package:

  1. Use getPOSIXTime, which returns a NominalDiffTime. This represents a fractional number of seconds, with a precision of 10-12 seconds.

  2. Multiply 1000 to convert from seconds to milliseconds.

  3. NominalDiffTime implements the RealFrac class, so you can use round to convert to an integer.


import Data.Time.Clock.POSIX (getPOSIXTime)

λ> (round . (* 1000)) <$> getPOSIXTime
1489694668011

Upvotes: 21

Related Questions