module Network.Wai.Handler.Warp.Date (
withDateCache
, GMTDate
) where
#if __GLASGOW_HASKELL__ < 709
import Control.Applicative
#endif
import Control.AutoUpdate (defaultUpdateSettings, updateAction, mkAutoUpdate)
import Data.ByteString.Char8
import Network.HTTP.Date
#if WINDOWS
import Data.Time (UTCTime, getCurrentTime)
import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds)
import Foreign.C.Types (CTime(..))
#else
import System.Posix (epochTime)
#endif
type GMTDate = ByteString
withDateCache :: (IO GMTDate -> IO a) -> IO a
withDateCache action = initialize >>= action
initialize :: IO (IO GMTDate)
initialize = mkAutoUpdate defaultUpdateSettings {
updateAction = formatHTTPDate <$> getCurrentHTTPDate
}
#ifdef WINDOWS
uToH :: UTCTime -> HTTPDate
uToH = epochTimeToHTTPDate . CTime . truncate . utcTimeToPOSIXSeconds
getCurrentHTTPDate :: IO HTTPDate
getCurrentHTTPDate = uToH <$> getCurrentTime
#else
getCurrentHTTPDate :: IO HTTPDate
getCurrentHTTPDate = epochTimeToHTTPDate <$> epochTime
#endif