]> gitweb @ CieloNegro.org - time-http.git/blob - Data/Time/RFC1123.hs
Finished docs
[time-http.git] / Data / Time / RFC1123.hs
1 -- |This module provides functions to parse and format RFC 1123 date
2 -- and time formats.
3 --
4 -- The format is basically same as RFC 822, but the syntax for @date@
5 -- is changed from:
6 --
7 -- > year ::= 2DIGIT
8 --
9 -- to:
10 --
11 -- > year ::= 4DIGIT
12 module Data.Time.RFC1123
13     ( format
14     , parse
15     )
16     where
17
18 import qualified Text.Parsec as P
19
20 import Data.Time
21 import Data.Time.Calendar.WeekDate
22 import Data.Time.HTTP.Common
23 import Data.Time.RFC822 (showRFC822TimeZone)
24 import Data.Time.RFC1123.Parsec
25
26 -- |Format a 'ZonedTime' in RFC 1123.
27 format :: ZonedTime -> String
28 format zonedTime
29     = let localTime          = zonedTimeToLocalTime zonedTime
30           timeZone           = zonedTimeZone zonedTime
31           (year, month, day) = toGregorian (localDay localTime)
32           (_, _, week)       = toWeekDate  (localDay localTime)
33           timeOfDay          = localTimeOfDay localTime
34       in
35         concat [ shortWeekDayName week
36                , ", "
37                , show2 day
38                , " "
39                , shortMonthName month
40                , " "
41                , show4 year
42                , " "
43                , show2 (todHour timeOfDay)
44                , ":"
45                , show2 (todMin timeOfDay)
46                , ":"
47                , show2 (floor (todSec timeOfDay))
48                , " "
49                , showRFC822TimeZone timeZone
50                ]
51
52 -- |Parse an RFC 1123 date and time string. When the string can't be
53 -- parsed, it returns 'Nothing'.
54 parse :: String -> Maybe ZonedTime
55 parse src = case P.parse p "" src of
56               Right zt -> Just zt
57               Left  _  -> Nothing
58     where
59       p = do zt <- rfc1123DateAndTime
60              _  <- P.eof
61              return zt