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 'System.Time.CalendarTime' to RFC 1123 Date and Time
24 formatRFC1123DateTime :: CalendarTime -> String
25 formatRFC1123DateTime time
28 id (week !! fromEnum (ctWDay time))
32 id (month !! fromEnum (ctMonth time))
34 fmtDec 4 (ctYear time)
36 fmtDec 2 (ctHour time)
45 -- |Format a 'System.Time.ClockTime' to HTTP Date and Time. Time zone
46 -- will be always UTC but prints as GMT.
47 formatHTTPDateTime :: ClockTime -> String
48 formatHTTPDateTime time
50 formatRFC1123DateTime $! (\cal -> cal { ctTZName = "GMT" }) $! toUTCTime time
52 -- |Parse an HTTP Date and Time.
54 -- Limitation: RFC 2616 (HTTP\/1.1) says we must accept these three
57 -- * @Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123@
59 -- * @Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036@
61 -- * @Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format@
63 -- ...but currently this function only supports the RFC 1123
64 -- format. This is a violation of RFC 2616 so this should be fixed
65 -- later. What a bother!
66 parseHTTPDateTime :: String -> Maybe ClockTime
68 = case parseStr httpDateTime src of
69 (# Success ct, _ #) -> Just ct
70 (# _ , _ #) -> Nothing
73 httpDateTime :: Parser ClockTime
74 httpDateTime = do foldl (<|>) failP (map string week)
77 day <- liftM read (count 2 digit)
79 mon <- foldl (<|>) failP (map tryEqToFst (zip month [1..]))
81 year <- liftM read (count 4 digit)
83 hour <- liftM read (count 2 digit)
85 min <- liftM read (count 2 digit)
87 sec <- liftM read (count 2 digit)
91 return $ toClockTime $ CalendarTime {
93 , ctMonth = toEnum (mon - 1)
102 , ctTZName = undefined
103 , ctIsDST = undefined
106 tryEqToFst :: (String, a) -> Parser a
107 tryEqToFst (str, a) = string str >> return a