For Developers
How to get the current timestamp, convert it to a date, and convert a date back to a timestamp in Haskell, using the time package's POSIX time functions.
import Data.Time.Clock.POSIX (getPOSIXTime)
main = do
ts <- getPOSIXTime
print ts -- includes sub-second precision
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
let utcTime = posixSecondsToUTCTime 1750000000
import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds)
import Data.Time (UTCTime(..), fromGregorian)
let ts = utcTimeToPOSIXSeconds (UTCTime (fromGregorian 2025 6 15) (12*3600))
getPOSIXTime returns a POSIXTime, which is a type alias for NominalDiffTime — not a plain number — and it carries sub-second precision by default rather than rounding to whole seconds the way many other languages' "current timestamp" functions do. Code that needs a plain integer count of seconds has to explicitly truncate or round it (commonly with round or floor), and forgetting that step means comparisons or storage that expect a clean integer end up with unexpected fractional values instead.
Paste it into the Timestamp ⇄ Date Converter to check it instantly, in any timezone, without writing any code. Working across several languages on the same project? The full Epoch Time in Programming Languages guide has all of them side by side for quick comparison.
Related
Every language's snippets side by side, for quick comparison.
Convert any timestamp instantly, no code required.
Another strongly-typed language with an explicit, non-primitive time type.