]> gitweb @ CieloNegro.org - time-http.git/blobdiff - Data/Time/RFC822.hs
Data.Time.RFC822 now compiles.
[time-http.git] / Data / Time / RFC822.hs
index 4b3d91cf8799714e45eb15c1dfe16e5b77c72d22..152d99290f57e89915e72eaf80efd36e72c22e1e 100644 (file)
@@ -1,91 +1,61 @@
+{-# LANGUAGE
+    UnicodeSyntax
+  #-}
+-- |This module provides functions to parse and format RFC 822 date
+-- and time formats.
+--
+-- The syntax is as follows:
+--
+-- > date-time   ::= [ day-of-week ", " ] date SP time SP zone
+-- > day-of-week ::= "Mon" | "Tue" | "Wed" | "Thu"
+-- >               | "Fri" | "Sat" | "Sun"
+-- > date        ::= day SP month SP year
+-- > day         ::= 2DIGIT
+-- > year        ::= 2DIGIT             ; Yes, only 2 digits.
+-- > month       ::= "Jan" | "Feb" | "Mar" | "Apr"
+-- >               | "May" | "Jun" | "Jul" | "Aug"
+-- >               | "Sep" | "Oct" | "Nov" | "Dec"
+-- > time        ::= hour ":" minute [ ":" second ]
+-- > hour        ::= 2DIGIT
+-- > minute      ::= 2DIGIT
+-- > second      ::= 2DIGIT
+-- > zone        ::= "UT"  | "GMT"      ; Universal Time
+-- >               | "EST" | "EDT"      ; Eastern : -5 / -4
+-- >               | "CST" | "CDT"      ; Central : -6 / -5
+-- >               | "MST" | "MDT"      ; Mountain: -7 / -6
+-- >               | "PST" | "PDT"      ; Pacific : -8 / -7
+-- >               | "Z"                ; UT
+-- >               | "A"                ;  -1
+-- >               | "M"                ; -12
+-- >               | "N"                ;  +1
+-- >               | "Y"                ; +12
+-- >               | ("+" | "-") 4DIGIT ; Local diff: HHMM
 module Data.Time.RFC822
-    ( format
-    , parse
+    ( -- * Formatting
+      toAscii
+    , toAsciiBuilder
+
+      -- * Parsing
+    , fromAscii
+    , rfc822DateAndTime
     )
     where
-
-import qualified Text.Parsec as P
-
+import Data.Ascii (Ascii)
+import qualified Data.Ascii as A
+import qualified Data.Attoparsec.Char8 as P
 import Data.Time
-import Data.Time.Calendar.WeekDate
-import Data.Time.HTTP.Common
-import Data.Time.RFC822.Parsec
-
-
-{-
-     date-time   =  [ day "," ] date time        ; dd mm yy
-                                                 ;  hh:mm:ss zzz
-
-     day         =  "Mon"  / "Tue" /  "Wed"  / "Thu"
-                 /  "Fri"  / "Sat" /  "Sun"
-
-     date        =  1*2DIGIT month 2DIGIT        ; day month year
-                                                 ;  e.g. 20 Jun 82
+import Data.Time.RFC822.Internal
+import Prelude.Unicode
 
-     month       =  "Jan"  /  "Feb" /  "Mar"  /  "Apr"
-                 /  "May"  /  "Jun" /  "Jul"  /  "Aug"
-                 /  "Sep"  /  "Oct" /  "Nov"  /  "Dec"
-
-     time        =  hour zone                    ; ANSI and Military
-
-     hour        =  2DIGIT ":" 2DIGIT [":" 2DIGIT]
-                                                 ; 00:00:00 - 23:59:59
-
-     zone        =  "UT"  / "GMT"                ; Universal Time
-                                                 ; North American : UT
-                 /  "EST" / "EDT"                ;  Eastern:  - 5/ - 4
-                 /  "CST" / "CDT"                ;  Central:  - 6/ - 5
-                 /  "MST" / "MDT"                ;  Mountain: - 7/ - 6
-                 /  "PST" / "PDT"                ;  Pacific:  - 8/ - 7
-                 /  1ALPHA                       ; Military: Z = UT;
-                                                 ;  A:-1; (J not used)
-                                                 ;  M:-12; N:+1; Y:+12
-                 / ( ("+" / "-") 4DIGIT )        ; Local differential
-                                                 ;  hours+min. (HHMM)
--}
-
-format :: ZonedTime -> String
-format zonedTime
-    = let localTime          = zonedTimeToLocalTime zonedTime
-          timeZone           = zonedTimeZone zonedTime
-          (year, month, day) = toGregorian (localDay localTime)
-          (_, _, week)       = toWeekDate  (localDay localTime)
-          timeOfDay          = localTimeOfDay localTime
-      in
-        concat [ shortWeekDayName week
-               , ", "
-               , show2 day
-               , " "
-               , shortMonthName month
-               , " "
-               , show2 (year `mod` 100)
-               , " "
-               , show2 (todHour timeOfDay)
-               , ":"
-               , show2 (todMin timeOfDay)
-               , ":"
-               , show2 (floor (todSec timeOfDay))
-               , " "
-               , showTZ timeZone
-               ]
-
-showTZ :: TimeZone -> String
-showTZ tz
-    = case timeZoneMinutes tz of
-        offset | offset <  0 -> '-' : showTZ' (negate offset)
-               | otherwise   -> '+' : showTZ' offset
-    where
-      showTZ' offset
-          = let h = offset `div` 60
-                m = offset - h * 60
-            in
-              concat [show2 h, show2 m]
+-- |Convert a 'ZonedTime' to RFC 822 date and time string.
+toAscii ∷ ZonedTime → Ascii
+toAscii = A.fromAsciiBuilder ∘ toAsciiBuilder
 
-parse :: String -> Maybe ZonedTime
-parse src = case P.parse p "" src of
-              Right zt -> Just zt
-              Left  _  -> Nothing
+-- |Parse an RFC 822 date and time string. When the string can't be
+-- parsed, it returns @'Left' err@.
+fromAscii ∷ Ascii → Either String ZonedTime
+fromAscii = P.parseOnly p ∘ A.toByteString
     where
-      p = do zt <- parser
-             _  <- P.eof
+      p = do zt ← rfc822DateAndTime
+             P.endOfInput
              return zt