    {-# LANGUAGE Safe #-}

    module Data.Time.Clock.Internal.CTimeval where
    #ifndef mingw32_HOST_OS
    -- All Unix-specific, this
    import Foreign
    import Foreign.C

    data CTimeval =
        MkCTimeval CLong
                   CLong

    instance Storable CTimeval where
        sizeOf :: CTimeval -> Int
    sizeOf CTimeval
    _ = (forall a. Storable a => a -> Int
    sizeOf (forall a. HasCallStack => a
    undefined :: CLong)) forall a. Num a => a -> a -> a
    * Int
    2
        alignment :: CTimeval -> Int
    alignment CTimeval
    _ = forall a. Storable a => a -> Int
    alignment (forall a. HasCallStack => a
    undefined :: CLong)
        peek :: Ptr CTimeval -> IO CTimeval
    peek Ptr CTimeval
    p = do
            CLong
    s <- forall a. Storable a => Ptr a -> Int -> IO a
    peekElemOff (forall a b. Ptr a -> Ptr b
    castPtr Ptr CTimeval
    p) Int
    0
            CLong
    mus <- forall a. Storable a => Ptr a -> Int -> IO a
    peekElemOff (forall a b. Ptr a -> Ptr b
    castPtr Ptr CTimeval
    p) Int
    1
            forall (m :: * -> *) a. Monad m => a -> m a
    return (CLong -> CLong -> CTimeval
    MkCTimeval CLong
    s CLong
    mus)
        poke :: Ptr CTimeval -> CTimeval -> IO ()
    poke Ptr CTimeval
    p (MkCTimeval CLong
    s CLong
    mus) = do
            forall a. Storable a => Ptr a -> Int -> a -> IO ()
    pokeElemOff (forall a b. Ptr a -> Ptr b
    castPtr Ptr CTimeval
    p) Int
    0 CLong
    s
            forall a. Storable a => Ptr a -> Int -> a -> IO ()
    pokeElemOff (forall a b. Ptr a -> Ptr b
    castPtr Ptr CTimeval
    p) Int
    1 CLong
    mus

    foreign import ccall unsafe "time.h gettimeofday" gettimeofday :: Ptr CTimeval -> Ptr () -> IO CInt

    -- | Get the current POSIX time from the system clock.
    getCTimeval :: IO CTimeval
    getCTimeval :: IO CTimeval
    getCTimeval =
        forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
    with
            (CLong -> CLong -> CTimeval
    MkCTimeval CLong
    0 CLong
    0)
            (\Ptr CTimeval
    ptval -> do
                 forall a. (Eq a, Num a) => String -> IO a -> IO ()
    throwErrnoIfMinus1_ String
    "gettimeofday" forall a b. (a -> b) -> a -> b
    $ Ptr CTimeval -> Ptr () -> IO CInt
    gettimeofday Ptr CTimeval
    ptval forall a. Ptr a
    nullPtr
                 forall a. Storable a => Ptr a -> IO a
    peek Ptr CTimeval
    ptval)
    #endif