1 -- |This module parses and prints RFC 1123 Date and Time string.
3 -- In general you don't have to use this module directly.
4 module Network.HTTP.Lucu.RFC1123DateTime
5 ( formatRFC1123DateTime
12 import Network.HTTP.Lucu.Format
13 import Network.HTTP.Lucu.Parser
17 month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
20 week = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
22 -- |Format a @CalendarTime@ to RFC 1123 Date and Time string.
23 formatRFC1123DateTime :: CalendarTime -> String
24 formatRFC1123DateTime time
27 id (week !! fromEnum (ctWDay time))
31 id (month !! fromEnum (ctMonth time))
33 fmtDec 4 (ctYear time)
35 fmtDec 2 (ctHour time)
44 -- |Format a @ClockTime@ to HTTP Date and Time. Time zone will be
45 -- always UTC but prints as GMT.
46 formatHTTPDateTime :: ClockTime -> String
47 formatHTTPDateTime time
49 formatRFC1123DateTime $! (\cal -> cal { ctTZName = "GMT" }) $! toUTCTime time
51 -- |Parse an HTTP Date and Time.
53 -- Limitation: RFC 2616 (HTTP\/1.1) says we must accept these three
56 -- * @Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123@
58 -- * @Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036@
60 -- * @Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format@
62 -- ...but currently this function only supports the RFC 1123
63 -- format. This is a violation of RFC 2616 so this should be fixed
64 -- later. What a bother!
65 parseHTTPDateTime :: String -> Maybe ClockTime
67 = case parseStr httpDateTime src of
68 (Success ct, _) -> Just ct
72 httpDateTime :: Parser ClockTime
73 httpDateTime = do foldl (<|>) (fail "") (map string week)
76 day <- liftM read (count 2 digit)
78 mon <- foldl (<|>) (fail "") (map tryEqToFst (zip month [1..]))
80 year <- liftM read (count 4 digit)
82 hour <- liftM read (count 2 digit)
84 min <- liftM read (count 2 digit)
86 sec <- liftM read (count 2 digit)
90 return $ toClockTime $ CalendarTime {
92 , ctMonth = toEnum (mon - 1)
101 , ctTZName = undefined
102 , ctIsDST = undefined
105 tryEqToFst :: (String, a) -> Parser a
106 tryEqToFst (str, a) = string str >> return a