- = case getHeader "Date" res of
- Nothing -> do time <- getClockTime
- return $ addHeader "Date" (formatHTTPDateTime time) res
- Just _ -> return res
\ No newline at end of file
+ = case getHeader (C8.pack "Date") res of
+ Nothing -> do date <- getCurrentDate
+ return $ setHeader (C8.pack "Date") date res
+ Just _ -> return res
+
+
+cache :: IORef (ClockTime, ByteString)
+cache = unsafePerformIO $
+ newIORef (TOD 0 0, undefined)
+{-# NOINLINE cache #-}
+
+getCurrentDate :: IO ByteString
+getCurrentDate = do now@(TOD curSec _) <- getClockTime
+ (TOD cachedSec _, cachedStr) <- readIORef cache
+
+ if curSec == cachedSec then
+ return cachedStr
+ else
+ do let dateStr = C8.pack $ formatHTTPDateTime now
+ writeIORef cache (now, dateStr)
+ return dateStr
\ No newline at end of file