module Network.Wai.Logger.Date (
DateCacheGetter
, DateCacheUpdater
, ZonedDate
, DateCacheConf(..)
, zonedDateCacheConf
, clockDateCacher
) where
import Control.AutoUpdate (mkAutoUpdate, defaultUpdateSettings, updateAction)
import Data.ByteString (ByteString)
#if WINDOWS
import qualified Data.ByteString.Char8 as BS
import Data.Time (UTCTime, formatTime, getCurrentTime, utcToLocalZonedTime)
# if MIN_VERSION_time(1,5,0)
import Data.Time (defaultTimeLocale)
# else
import System.Locale (defaultTimeLocale)
# endif
#else
import Data.UnixTime (formatUnixTime, fromEpochTime)
import System.Posix (EpochTime, epochTime)
#endif
type DateCacheGetter = IO ZonedDate
type DateCacheUpdater = IO ()
type ZonedDate = ByteString
data DateCacheConf t = DateCacheConf {
getTime :: IO t
, formatDate :: t -> IO ByteString
}
#if WINDOWS
zonedDateCacheConf :: DateCacheConf UTCTime
zonedDateCacheConf = DateCacheConf {
getTime = getCurrentTime
, formatDate = \ut -> do
zt <- utcToLocalZonedTime ut
return $ BS.pack $ formatTime defaultTimeLocale "%d/%b/%Y:%T %z" zt
}
#else
zonedDateCacheConf :: DateCacheConf EpochTime
zonedDateCacheConf = DateCacheConf {
getTime = epochTime
, formatDate = formatUnixTime "%d/%b/%Y:%T %z" . fromEpochTime
}
#endif
clockDateCacher :: IO (DateCacheGetter, DateCacheUpdater)
clockDateCacher = do
getter <- mkAutoUpdate defaultUpdateSettings
{ updateAction = getTime zonedDateCacheConf
>>= formatDate zonedDateCacheConf
}
return (getter, return ())