{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP          #-}

module Snap.Internal.Http.Server.Date
  ( getDateString
  , getLogDateString
  ) where

------------------------------------------------------------------------------
import           Control.Exception        (mask_)
import           Control.Monad            (when)
import           Data.ByteString          (ByteString)
import           Data.IORef               (IORef, newIORef, readIORef, writeIORef)
import           Foreign.C.Types          (CTime)
import           System.IO.Unsafe         (unsafePerformIO)
import           System.PosixCompat.Time  (epochTime)
------------------------------------------------------------------------------
import           Snap.Internal.Http.Types (formatHttpTime, formatLogTime)


------------------------------------------------------------------------------
data DateState = DateState {
      DateState -> IORef ByteString
_cachedDateString :: !(IORef ByteString)
    , DateState -> IORef ByteString
_cachedLogString  :: !(IORef ByteString)
    , DateState -> IORef CTime
_lastFetchTime    :: !(IORef CTime)
    }


------------------------------------------------------------------------------
dateState :: DateState
dateState :: DateState
dateState = IO DateState -> DateState
forall a. IO a -> a
unsafePerformIO (IO DateState -> DateState) -> IO DateState -> DateState
forall a b. (a -> b) -> a -> b
$ do
    (ByteString
s1, ByteString
s2, CTime
date) <- IO (ByteString, ByteString, CTime)
fetchTime
    IORef ByteString
bs1 <- ByteString -> IO (IORef ByteString)
forall a. a -> IO (IORef a)
newIORef (ByteString -> IO (IORef ByteString))
-> ByteString -> IO (IORef ByteString)
forall a b. (a -> b) -> a -> b
$! ByteString
s1
    IORef ByteString
bs2 <- ByteString -> IO (IORef ByteString)
forall a. a -> IO (IORef a)
newIORef (ByteString -> IO (IORef ByteString))
-> ByteString -> IO (IORef ByteString)
forall a b. (a -> b) -> a -> b
$! ByteString
s2
    IORef CTime
dt  <- CTime -> IO (IORef CTime)
forall a. a -> IO (IORef a)
newIORef (CTime -> IO (IORef CTime)) -> CTime -> IO (IORef CTime)
forall a b. (a -> b) -> a -> b
$! CTime
date

    DateState -> IO DateState
forall (m :: * -> *) a. Monad m => a -> m a
return (DateState -> IO DateState) -> DateState -> IO DateState
forall a b. (a -> b) -> a -> b
$! IORef ByteString -> IORef ByteString -> IORef CTime -> DateState
DateState IORef ByteString
bs1 IORef ByteString
bs2 IORef CTime
dt
{-# NOINLINE dateState #-}


------------------------------------------------------------------------------
fetchTime :: IO (ByteString,ByteString,CTime)
fetchTime :: IO (ByteString, ByteString, CTime)
fetchTime = do
    !CTime
now <- IO CTime
epochTime
    !ByteString
t1  <- CTime -> IO ByteString
formatHttpTime CTime
now
    !ByteString
t2  <- CTime -> IO ByteString
formatLogTime CTime
now
    let !out :: (ByteString, ByteString, CTime)
out = (ByteString
t1, ByteString
t2, CTime
now)
    (ByteString, ByteString, CTime)
-> IO (ByteString, ByteString, CTime)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString, ByteString, CTime)
out


------------------------------------------------------------------------------
updateState :: DateState -> IO ()
updateState :: DateState -> IO ()
updateState (DateState IORef ByteString
dateString IORef ByteString
logString IORef CTime
time) = do
    (ByteString
s1, ByteString
s2, CTime
now) <- IO (ByteString, ByteString, CTime)
fetchTime
    IORef ByteString -> ByteString -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef ByteString
dateString (ByteString -> IO ()) -> ByteString -> IO ()
forall a b. (a -> b) -> a -> b
$! ByteString
s1
    IORef ByteString -> ByteString -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef ByteString
logString  (ByteString -> IO ()) -> ByteString -> IO ()
forall a b. (a -> b) -> a -> b
$! ByteString
s2
    IORef CTime -> CTime -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef CTime
time       (CTime -> IO ()) -> CTime -> IO ()
forall a b. (a -> b) -> a -> b
$! CTime
now

    () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return (() -> IO ()) -> () -> IO ()
forall a b. (a -> b) -> a -> b
$! ()


------------------------------------------------------------------------------
ensureFreshDate :: IO ()
ensureFreshDate :: IO ()
ensureFreshDate = IO () -> IO ()
forall a. IO a -> IO a
mask_ (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
    CTime
now <- IO CTime
epochTime
    CTime
old <- IORef CTime -> IO CTime
forall a. IORef a -> IO a
readIORef (IORef CTime -> IO CTime) -> IORef CTime -> IO CTime
forall a b. (a -> b) -> a -> b
$ DateState -> IORef CTime
_lastFetchTime DateState
dateState
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (CTime
now CTime -> CTime -> Bool
forall a. Ord a => a -> a -> Bool
> CTime
old) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$! DateState -> IO ()
updateState DateState
dateState


------------------------------------------------------------------------------
getDateString :: IO ByteString
getDateString :: IO ByteString
getDateString = IO ByteString -> IO ByteString
forall a. IO a -> IO a
mask_ (IO ByteString -> IO ByteString) -> IO ByteString -> IO ByteString
forall a b. (a -> b) -> a -> b
$ do
    IO ()
ensureFreshDate
    IORef ByteString -> IO ByteString
forall a. IORef a -> IO a
readIORef (IORef ByteString -> IO ByteString)
-> IORef ByteString -> IO ByteString
forall a b. (a -> b) -> a -> b
$ DateState -> IORef ByteString
_cachedDateString DateState
dateState


------------------------------------------------------------------------------
getLogDateString :: IO ByteString
getLogDateString :: IO ByteString
getLogDateString = IO ByteString -> IO ByteString
forall a. IO a -> IO a
mask_ (IO ByteString -> IO ByteString) -> IO ByteString -> IO ByteString
forall a b. (a -> b) -> a -> b
$ do
    IO ()
ensureFreshDate
    IORef ByteString -> IO ByteString
forall a. IORef a -> IO a
readIORef (IORef ByteString -> IO ByteString)
-> IORef ByteString -> IO ByteString
forall a b. (a -> b) -> a -> b
$ DateState -> IORef ByteString
_cachedLogString DateState
dateState