2

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.

asked Mar 24, 2013 at 16:40
1

2 Answers 2

6

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.

answered Mar 24, 2013 at 17:19
Sign up to request clarification or add additional context in comments.

10 Comments

Perhaps worth saying that this uses Data.Time.Clock (though that can easily be Hoogled).
Also, the toRational isn't necessary: floor is already polymorphic on any RealFrac argument.
Hmm, I'm getting this error: No instance for (RealFrac DiffTime) arising from a use of `floor'
Weird,what version of GHC/Data.time do you have? Otherwise you can always use th emroe verbose floor.toRational
I'm using ghc 7.04. I also have a portable version of 7.6.2 and wierdly, it compiles with 7.6.2. However I can't use this version because it doesn't have the System.Random library included. floor.toRational spews more errors. Maybe I should just use 7.6.2 but I'll need to find a way to import System.Random, I didn't have any luck when I tried to import it before
|
0

My 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

answered Mar 24, 2013 at 17:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.