{-# LANGUAGE CPP #-}
{-# OPTIONS -fno-cse #-}
module Happstack.Server.Internal.Clock
    ( getApproximateTime
    , getApproximatePOSIXTime
    , getApproximateUTCTime
    , formatHttpDate
    ) where

import Control.Concurrent
import Control.Monad
import Data.IORef
import Data.Time.Clock       (UTCTime)
import Data.Time.Clock.POSIX (POSIXTime, getPOSIXTime, posixSecondsToUTCTime)
import System.IO.Unsafe

#if MIN_VERSION_time(1,5,0)
import Data.Time.Format (formatTime, defaultTimeLocale)
#else
import Data.Time.Format (formatTime)
import System.Locale    (defaultTimeLocale)
#endif

import qualified Data.ByteString.Char8 as B

data DateCache = DateCache {
      DateCache -> IORef POSIXTime
cachedPOSIXTime :: !(IORef POSIXTime)
    , DateCache -> IORef ByteString
cachedHttpDate  :: !(IORef B.ByteString)
    }

formatHttpDate :: UTCTime -> String
formatHttpDate :: UTCTime -> String
formatHttpDate = forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale String
"%a, %d %b %Y %X GMT"
{-# INLINE formatHttpDate #-}

mkTime :: IO (POSIXTime, B.ByteString)
mkTime :: IO (POSIXTime, ByteString)
mkTime =
    do POSIXTime
now <- IO POSIXTime
getPOSIXTime
       forall (m :: * -> *) a. Monad m => a -> m a
return (POSIXTime
now, String -> ByteString
B.pack forall a b. (a -> b) -> a -> b
$ UTCTime -> String
formatHttpDate (POSIXTime -> UTCTime
posixSecondsToUTCTime POSIXTime
now))

{-# NOINLINE clock #-}
clock :: DateCache
clock :: DateCache
clock = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ do
  (POSIXTime
now, ByteString
httpDate) <- IO (POSIXTime, ByteString)
mkTime
  IORef POSIXTime
nowRef      <- forall a. a -> IO (IORef a)
newIORef POSIXTime
now
  IORef ByteString
httpDateRef <- forall a. a -> IO (IORef a)
newIORef ByteString
httpDate
  let dateCache :: DateCache
dateCache = (IORef POSIXTime -> IORef ByteString -> DateCache
DateCache IORef POSIXTime
nowRef IORef ByteString
httpDateRef)
  forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ IO () -> IO ThreadId
forkIO forall a b. (a -> b) -> a -> b
$ DateCache -> IO ()
updater DateCache
dateCache
  forall (m :: * -> *) a. Monad m => a -> m a
return DateCache
dateCache

updater :: DateCache -> IO ()
updater :: DateCache -> IO ()
updater DateCache
dateCache =
    do Int -> IO ()
threadDelay (Int
10forall a b. (Num a, Integral b) => a -> b -> a
^(Int
6 :: Int)) -- Every second
       (POSIXTime
now, ByteString
httpDate) <- IO (POSIXTime, ByteString)
mkTime
       forall a. IORef a -> a -> IO ()
writeIORef (DateCache -> IORef POSIXTime
cachedPOSIXTime DateCache
dateCache) POSIXTime
now
       forall a. IORef a -> a -> IO ()
writeIORef (DateCache -> IORef ByteString
cachedHttpDate  DateCache
dateCache) ByteString
httpDate
       DateCache -> IO ()
updater DateCache
dateCache

getApproximateTime :: IO B.ByteString
getApproximateTime :: IO ByteString
getApproximateTime = forall a. IORef a -> IO a
readIORef (DateCache -> IORef ByteString
cachedHttpDate DateCache
clock)

getApproximatePOSIXTime :: IO POSIXTime
getApproximatePOSIXTime :: IO POSIXTime
getApproximatePOSIXTime = forall a. IORef a -> IO a
readIORef (DateCache -> IORef POSIXTime
cachedPOSIXTime DateCache
clock)

getApproximateUTCTime :: IO UTCTime
getApproximateUTCTime :: IO UTCTime
getApproximateUTCTime = POSIXTime -> UTCTime
posixSecondsToUTCTime forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO POSIXTime
getApproximatePOSIXTime