    {-# LINE 1 "src/Data/Thyme/Clock/POSIX.hsc" #-}
    {-# LANGUAGE ForeignFunctionInterface #-}

    {-# LINE 4 "src/Data/Thyme/Clock/POSIX.hsc" #-}

    {-# LINE 6 "src/Data/Thyme/Clock/POSIX.hsc" #-}

    module Data.Thyme.Clock.POSIX
        ( posixDayLength
        , POSIXTime
        , posixTime
        , getPOSIXTime
        ) where

    import Prelude
    import Control.Lens
    import Data.AdditiveGroup
    import Data.Thyme.Internal.Micro
    import Data.Thyme.Clock.Internal

    {-# LINE 23 "src/Data/Thyme/Clock/POSIX.hsc" #-}
    import Foreign.C.Error (throwErrnoIfMinus1_)
    import Foreign.C.Types
    import Foreign.Marshal.Alloc (allocaBytes)
    import Foreign.Ptr (Ptr, nullPtr)
    import Foreign.Storable

    {-# LINE 29 "src/Data/Thyme/Clock/POSIX.hsc" #-}

    type POSIXTime = NominalDiffTime

    {-# INLINE posixTime #-}
    posixTime :: Iso' UTCTime POSIXTime
    posixTime :: Overloaded p f UTCTime UTCTime POSIXTime POSIXTime
    posixTime = (UTCTime -> POSIXTime)
    -> (POSIXTime -> UTCTime)
    -> Iso UTCTime UTCTime POSIXTime POSIXTime
    forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
    iso (\ (UTCRep t :: POSIXTime
    t) -> POSIXTime
    t POSIXTime -> POSIXTime -> POSIXTime
    forall v. AdditiveGroup v => v -> v -> v
    ^-^ POSIXTime
    unixEpoch)
            (POSIXTime -> UTCTime
    UTCRep (POSIXTime -> UTCTime)
    -> (POSIXTime -> POSIXTime) -> POSIXTime -> UTCTime
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . POSIXTime -> POSIXTime -> POSIXTime
    forall v. AdditiveGroup v => v -> v -> v
    (^+^) POSIXTime
    unixEpoch) where
        unixEpoch :: POSIXTime
    unixEpoch = AReview POSIXTime POSIXTime Int64 Int64 -> Int64 -> POSIXTime
    forall s t a b. AReview s t a b -> b -> t
    review AReview POSIXTime POSIXTime Int64 Int64
    forall t. TimeDiff t => Iso' t Int64
    microseconds (Int64 -> POSIXTime) -> Int64 -> POSIXTime
    forall a b. (a -> b) -> a -> b
    $
            {-ModifiedJulianDay-}40587 Int64 -> Int64 -> Int64
    forall a. Num a => a -> a -> a
    * {-posixDayLength-}86400000000

    {-# INLINE getPOSIXTime #-}
    getPOSIXTime :: IO POSIXTime

    {-# LINE 54 "src/Data/Thyme/Clock/POSIX.hsc" #-}

    getPOSIXTime :: IO POSIXTime
    getPOSIXTime = Int -> (Ptr () -> IO POSIXTime) -> IO POSIXTime
    forall a b. Int -> (Ptr a -> IO b) -> IO b
    allocaBytes (16) ((Ptr () -> IO POSIXTime) -> IO POSIXTime)
    -> (Ptr () -> IO POSIXTime) -> IO POSIXTime
    forall a b. (a -> b) -> a -> b
    $ \ ptv :: Ptr ()
    ptv -> do
    {-# LINE 56 "src/Data/Thyme/Clock/POSIX.hsc" #-}
        throwErrnoIfMinus1_ "gettimeofday" $ gettimeofday ptv nullPtr
        CTime sec <- (\hsc_ptr -> peekByteOff hsc_ptr 0) ptv
    {-# LINE 58 "src/Data/Thyme/Clock/POSIX.hsc" #-}
        CSUSeconds usec <- (\hsc_ptr -> peekByteOff hsc_ptr 8) ptv
    {-# LINE 59 "src/Data/Thyme/Clock/POSIX.hsc" #-}
        return . NominalDiffTime . Micro $
            1000000 * fromIntegral sec + fromIntegral usec

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

    {-# LINE 66 "src/Data/Thyme/Clock/POSIX.hsc" #-}