1 {-# LANGUAGE ForeignFunctionInterface #-}
3 module System.Event.Clock (getCurrentTime) where
7 import Foreign (Ptr, Storable(..), nullPtr, with)
8 import Foreign.C.Error (throwErrnoIfMinus1_)
9 import Foreign.C.Types (CInt, CLong)
15 -- TODO: Implement this for Windows.
17 -- | Return the current time, in seconds since Jan. 1, 1970.
18 getCurrentTime :: IO Double
20 tv <- with (CTimeval 0 0) $ \tvptr -> do
21 throwErrnoIfMinus1_ "gettimeofday" (gettimeofday tvptr nullPtr)
23 let !t = fromIntegral (sec tv) + fromIntegral (usec tv) / 1000000.0
26 ------------------------------------------------------------------------
29 data CTimeval = CTimeval
30 { sec :: {-# UNPACK #-} !CLong
31 , usec :: {-# UNPACK #-} !CLong
34 instance Storable CTimeval where
35 sizeOf _ = #size struct timeval
36 alignment _ = alignment (undefined :: CLong)
39 sec' <- #{peek struct timeval, tv_sec} ptr
40 usec' <- #{peek struct timeval, tv_usec} ptr
41 return $ CTimeval sec' usec'
44 #{poke struct timeval, tv_sec} ptr (sec tv)
45 #{poke struct timeval, tv_usec} ptr (usec tv)
47 foreign import ccall unsafe "sys/time.h gettimeofday" gettimeofday
48 :: Ptr CTimeval -> Ptr () -> IO CInt