tl;dr
Ruby's #to_i
method returns the number of seconds since the UNIX Epoch (January 1, 1970 00:00 UTC). You can return the same number with the following function:
secondsFromEpoch :: UTCTime -> Int
secondsFromEpoch utc =
let seconds = formatTime defaultTimeLocale "%s" utc
in read seconds
used as:
main :: IO ()
main = do
currentTime <- getCurrentTime
print $ secondsFromEpoch currentTime
Live demo
How does this work
To understand this solution you can just read the Date.Time.Format documentation, specifically at the formatTime
function:
%s
number of whole seconds since the Unix epoch. For times before the Unix epoch, this is a negative number. Note that in %s.%q and %s%Q the decimals are positive, not negative. For example, 0.9 seconds before the Unix epoch is formatted as -1.1 with %s%Q.
Therefore:
formatTime defaultTimeLocale "%s" utc
will return the number of seconds in a string. Since it's an integer, we can use read
and put it into an Int
.
Where was your mistake
An UTCTime
is composed of:
- a
Day
(a day)
- a
DiffTime
(time from midnight)
The function utctDayTime
has the following type:
utctDayTime :: UTCTime -> DiffTime
that is: it takes an UTCTime
and returns the number of seconds from midnight. Therefore utctDayTime
does not account for the day.