module Network.Wai.Handler.Warp.Date (
withDateCache
, getDate
, DateCache
, GMTDate
) where
import Control.Applicative
import Control.AutoUpdate (defaultUpdateSettings, updateAction, mkAutoUpdate)
import Data.ByteString.Char8
#if WINDOWS
import Data.Time (formatTime, getCurrentTime)
# if MIN_VERSION_time(1,5,0)
import Data.Time (defaultTimeLocale)
# else
import System.Locale (defaultTimeLocale)
# endif
#else
import Network.HTTP.Date
import System.Posix (epochTime)
#endif
type GMTDate = ByteString
type DateCache = IO GMTDate
withDateCache :: (DateCache -> IO a) -> IO a
withDateCache action = initialize >>= action
initialize :: IO DateCache
initialize = mkAutoUpdate defaultUpdateSettings { updateAction = getCurrentGMTDate }
getDate :: DateCache -> IO GMTDate
getDate = id
getCurrentGMTDate :: IO GMTDate
#ifdef WINDOWS
getCurrentGMTDate = formatDate <$> getCurrentTime
where
formatDate = pack . formatTime defaultTimeLocale "%a, %d %b %Y %H:%M:%S GMT"
#else
getCurrentGMTDate = formatHTTPDate . epochTimeToHTTPDate <$> epochTime
#endif