I've been searching for ages to try and find an easy way to take the current time (could be seconds since midnight, or hour, min, sec) to an int. Could anyone point me in the right direction? I've looked in the libraries and had no success.
-
I don't program in Haskell but have been looking for similar information in other languages. How about toSeconds? hackage.haskell.org/packages/archive/datetime/0.1/doc/html/…Tim– Tim2013年03月24日 16:43:42 +00:00Commented Mar 24, 2013 at 16:43
2 Answers 2
To get the time since midnight as an Int is pretty easy:
import Data.Time.Clock
main = do
currTime <- getCurrentTime
let timed = floor $ utctDayTime currTime :: Int
print timed
For kicks and giggles, here's a quick function for it
integralTime :: (Integral a) => IO a
integralTime = getCurrentTime >>= return.floor.utctDayTime
This works since TimeDiff (it's a RealFrac instance) can be floored to an integral.
10 Comments
Data.Time.Clock (though that can easily be Hoogled).toRational isn't necessary: floor is already polymorphic on any RealFrac argument.floor.toRationalMy first hit on Google when searching for "haskell datetime" was the same link as Tim gives. There you have getCurrentTime :: IO DateTime to get the current time, and then diffUTCTime :: UTCTime -> UTCTime -> NominalDiffTime to calculate a difference (treated as seconds).
http://hackage.haskell.org/packages/archive/datetime/0.1/doc/html/Data-DateTime.html http://hackage.haskell.org/packages/archive/time/1.1.4/doc/html/Data-Time-Clock.html#t:UTCTime