1 module Network.HTTP.Lucu.RFC1123DateTime
2 ( formatRFC1123DateTime -- CalendarTime -> String
3 , formatHTTPDateTime -- ClockTime -> String
4 , parseHTTPDateTime -- String -> Maybe ClockTime
11 import Text.ParserCombinators.Parsec
14 month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
15 week = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
17 formatRFC1123DateTime :: CalendarTime -> String
18 formatRFC1123DateTime time
19 = printf "%s, %02d %s %04d %02d:%02d:%02d %s"
20 (week !! fromEnum (ctWDay time))
22 (month !! fromEnum (ctMonth time))
30 formatHTTPDateTime :: ClockTime -> String
31 formatHTTPDateTime = formatRFC1123DateTime . (\cal -> cal { ctTZName = "GMT" }) . toUTCTime
34 parseHTTPDateTime :: String -> Maybe ClockTime
36 = case parse httpDateTime "" src of
40 httpDateTime :: Parser ClockTime
41 httpDateTime = do foldl (<|>) (unexpected "") (map (try . string) week)
44 day <- liftM read (count 2 digit)
46 mon <- foldl (<|>) (unexpected "") (map tryEqToFst (zip month [1..]))
48 year <- liftM read (count 4 digit)
50 hour <- liftM read (count 2 digit)
52 min <- liftM read (count 2 digit)
54 sec <- liftM read (count 2 digit)
58 return $ toClockTime $ CalendarTime {
60 , ctMonth = toEnum (mon - 1)
69 , ctTZName = undefined
73 tryEqToFst :: (String, a) -> Parser a
74 tryEqToFst (str, a) = try $ string str >> return a