    {-# LANGUAGE CPP #-}
    {-# 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
    _ = (CLong -> Int
    forall a. Storable a => a -> Int
    sizeOf (CLong
    forall a. HasCallStack => a
    undefined :: CLong)) Int -> Int -> Int
    forall a. Num a => a -> a -> a
    * Int
    2
        alignment :: CTimeval -> Int
    alignment CTimeval
    _ = CLong -> Int
    forall a. Storable a => a -> Int
    alignment (CLong
    forall a. HasCallStack => a
    undefined :: CLong)
        peek :: Ptr CTimeval -> IO CTimeval
    peek Ptr CTimeval
    p = do
            CLong
    s <- Ptr CLong -> Int -> IO CLong
    forall a. Storable a => Ptr a -> Int -> IO a
    peekElemOff (Ptr CTimeval -> Ptr CLong
    forall a b. Ptr a -> Ptr b
    castPtr Ptr CTimeval
    p) Int
    0
            CLong
    mus <- Ptr CLong -> Int -> IO CLong
    forall a. Storable a => Ptr a -> Int -> IO a
    peekElemOff (Ptr CTimeval -> Ptr CLong
    forall a b. Ptr a -> Ptr b
    castPtr Ptr CTimeval
    p) Int
    1
            CTimeval -> IO CTimeval
    forall a. a -> IO a
    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
            Ptr CLong -> Int -> CLong -> IO ()
    forall a. Storable a => Ptr a -> Int -> a -> IO ()
    pokeElemOff (Ptr CTimeval -> Ptr CLong
    forall a b. Ptr a -> Ptr b
    castPtr Ptr CTimeval
    p) Int
    0 CLong
    s
            Ptr CLong -> Int -> CLong -> IO ()
    forall a. Storable a => Ptr a -> Int -> a -> IO ()
    pokeElemOff (Ptr CTimeval -> Ptr CLong
    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 =
        CTimeval -> (Ptr CTimeval -> IO CTimeval) -> IO CTimeval
    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
                 String -> IO CInt -> IO ()
    forall a. (Eq a, Num a) => String -> IO a -> IO ()
    throwErrnoIfMinus1_ String
    "gettimeofday" (IO CInt -> IO ()) -> IO CInt -> IO ()
    forall a b. (a -> b) -> a -> b
    $ Ptr CTimeval -> Ptr () -> IO CInt
    gettimeofday Ptr CTimeval
    ptval Ptr ()
    forall a. Ptr a
    nullPtr
                 Ptr CTimeval -> IO CTimeval
    forall a. Storable a => Ptr a -> IO a
    peek Ptr CTimeval
    ptval)
    #endif