{-# LINE 1 "libraries/time/lib/Data/Time/Clock/Internal/CTimespec.hsc" #-}
module Data.Time.Clock.Internal.CTimespec where




{-# LINE 6 "libraries/time/lib/Data/Time/Clock/Internal/CTimespec.hsc" #-}

import Foreign
import Foreign.C
import System.IO.Unsafe



type ClockID = Int32
{-# LINE 14 "libraries/time/lib/Data/Time/Clock/Internal/CTimespec.hsc" #-}

data CTimespec = MkCTimespec CTime CLong

instance Storable CTimespec where
    sizeOf :: CTimespec -> Int
sizeOf CTimespec
_ = (Int
16)
{-# LINE 19 "libraries/time/lib/Data/Time/Clock/Internal/CTimespec.hsc" #-}
    alignment _ = alignment (undefined :: CLong)
    peek :: Ptr CTimespec -> IO CTimespec
peek Ptr CTimespec
p = do
        CTime
s  <- (\Ptr CTimespec
hsc_ptr -> forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr CTimespec
hsc_ptr Int
0) Ptr CTimespec
p
{-# LINE 22 "libraries/time/lib/Data/Time/Clock/Internal/CTimespec.hsc" #-}
        CLong
ns <- (\Ptr CTimespec
hsc_ptr -> forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr CTimespec
hsc_ptr Int
8) Ptr CTimespec
p
{-# LINE 23 "libraries/time/lib/Data/Time/Clock/Internal/CTimespec.hsc" #-}
        forall (m :: * -> *) a. Monad m => a -> m a
return (CTime -> CLong -> CTimespec
MkCTimespec CTime
s CLong
ns)
    poke :: Ptr CTimespec -> CTimespec -> IO ()
poke Ptr CTimespec
p (MkCTimespec CTime
s CLong
ns) = do
        (\Ptr CTimespec
hsc_ptr -> forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr CTimespec
hsc_ptr Int
0) Ptr CTimespec
p CTime
s
{-# LINE 26 "libraries/time/lib/Data/Time/Clock/Internal/CTimespec.hsc" #-}
        (\Ptr CTimespec
hsc_ptr -> forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr CTimespec
hsc_ptr Int
8) Ptr CTimespec
p CLong
ns
{-# LINE 27 "libraries/time/lib/Data/Time/Clock/Internal/CTimespec.hsc" #-}

foreign import ccall unsafe "time.h clock_gettime"
    clock_gettime :: ClockID -> Ptr CTimespec -> IO CInt
foreign import ccall unsafe "time.h clock_getres"
    clock_getres :: ClockID -> Ptr CTimespec -> IO CInt

-- | Get the resolution of the given clock.
clockGetRes :: Int32 -> IO (Either Errno CTimespec)
{-# LINE 35 "libraries/time/lib/Data/Time/Clock/Internal/CTimespec.hsc" #-}
clockGetRes clockid = alloca $ \ptspec -> do
    rc <- clock_getres clockid ptspec
    case rc of
        0 -> do
            res <- peek ptspec
            return $ Right res
        _ -> do
            errno <- getErrno
            return $ Left errno

-- | Get the current time from the given clock.
clockGetTime :: ClockID -> IO CTimespec
clockGetTime :: ClockID -> IO CTimespec
clockGetTime ClockID
clockid = forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca (\Ptr CTimespec
ptspec -> do
    forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwErrnoIfMinus1_ String
"clock_gettime" forall a b. (a -> b) -> a -> b
$ ClockID -> Ptr CTimespec -> IO CInt
clock_gettime ClockID
clockid Ptr CTimespec
ptspec
    forall a. Storable a => Ptr a -> IO a
peek Ptr CTimespec
ptspec
    )

clock_REALTIME :: ClockID
clock_REALTIME :: ClockID
clock_REALTIME = ClockID
0
{-# LINE 54 "libraries/time/lib/Data/Time/Clock/Internal/CTimespec.hsc" #-}

clock_TAI :: ClockID
clock_TAI :: ClockID
clock_TAI = ClockID
11
{-# LINE 57 "libraries/time/lib/Data/Time/Clock/Internal/CTimespec.hsc" #-}

realtimeRes :: CTimespec
realtimeRes :: CTimespec
realtimeRes = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ do
    Either Errno CTimespec
mres <- ClockID -> IO (Either Errno CTimespec)
clockGetRes ClockID
clock_REALTIME
    case Either Errno CTimespec
mres of
        Left Errno
errno -> forall a. IOError -> IO a
ioError (String -> Errno -> Maybe Handle -> Maybe String -> IOError
errnoToIOError String
"clock_getres" Errno
errno forall a. Maybe a
Nothing forall a. Maybe a
Nothing)
        Right CTimespec
res -> forall (m :: * -> *) a. Monad m => a -> m a
return CTimespec
res

clockResolution :: ClockID -> Maybe CTimespec
clockResolution :: ClockID -> Maybe CTimespec
clockResolution ClockID
clockid = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ do
    Either Errno CTimespec
mres <- ClockID -> IO (Either Errno CTimespec)
clockGetRes ClockID
clockid
    case Either Errno CTimespec
mres of
        Left Errno
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
        Right CTimespec
res -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just CTimespec
res


{-# LINE 73 "libraries/time/lib/Data/Time/Clock/Internal/CTimespec.hsc" #-}