1 module Network.HTTP.Lucu.RFC1123DateTime
2 ( formatRFC1123DateTime
9 import qualified Data.ByteString.Lazy.Char8 as B
10 import Data.ByteString.Lazy.Char8 (ByteString)
11 import Network.HTTP.Lucu.Parser
16 month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
17 week = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
19 formatRFC1123DateTime :: CalendarTime -> String
20 formatRFC1123DateTime time
21 = printf "%s, %02d %s %04d %02d:%02d:%02d %s"
22 (week !! fromEnum (ctWDay time))
24 (month !! fromEnum (ctMonth time))
32 formatHTTPDateTime :: ClockTime -> String
33 formatHTTPDateTime = formatRFC1123DateTime . (\cal -> cal { ctTZName = "GMT" }) . toUTCTime
36 parseHTTPDateTime :: String -> Maybe ClockTime
38 = case parseStr httpDateTime src of
39 (Success ct, _) -> Just ct
43 httpDateTime :: Parser ClockTime
44 httpDateTime = do foldl (<|>) (fail "") (map string week)
47 day <- liftM read (count 2 digit)
49 mon <- foldl (<|>) (fail "") (map tryEqToFst (zip month [1..]))
51 year <- liftM read (count 4 digit)
53 hour <- liftM read (count 2 digit)
55 min <- liftM read (count 2 digit)
57 sec <- liftM read (count 2 digit)
61 return $ toClockTime $ CalendarTime {
63 , ctMonth = toEnum (mon - 1)
72 , ctTZName = undefined
76 tryEqToFst :: (String, a) -> Parser a
77 tryEqToFst (str, a) = string str >> return a