]> gitweb @ CieloNegro.org - time-http.git/blobdiff - Data/Time/HTTP.hs
Data.Time.HTTP now compiles.
[time-http.git] / Data / Time / HTTP.hs
index 4352e8b0f02b2959964c9f56622e78896cc9113f..f106fc4c7f397f77466f75b3a986937ea68d16a3 100644 (file)
@@ -1,3 +1,6 @@
+{-# LANGUAGE
+    UnicodeSyntax
+  #-}
 -- |This module provides functions to parse and format HTTP\/1.1 date
 -- and time formats.
 --
 -- >              | "May" | "Jun" | "Jul" | "Aug"
 -- >              | "Sep" | "Oct" | "Nov" | "Dec"
 module Data.Time.HTTP
-    ( format
-    , parse
+    ( -- * Formatting
+      toAscii
+    , toAsciiBuilder
+
+      -- * Parsing
+    , fromAscii
+    , httpDateAndTime
     )
     where
-import qualified Data.Time.RFC1123 as RFC1123
+import Data.Ascii (Ascii)
+import qualified Data.Ascii as A
+import qualified Data.Attoparsec.Char8 as P
 import Data.Time
 import Data.Time.HTTP.Internal
+import Prelude.Unicode
 
--- |Format an 'UTCTime' in RFC 1123 date and time.
-format :: UTCTime -> String
-format utcTime
-    = let timeZone  = TimeZone 0 False "GMT"
-          zonedTime = utcToZonedTime timeZone utcTime
-      in
-        RFC1123.format zonedTime
+-- |Convert a 'UTCTime' to RFC 1123 date and time string.
+toAscii ∷ UTCTime → Ascii
+toAscii = A.fromAsciiBuilder ∘ toAsciiBuilder
 
 -- |Parse a date and time string in any of RFC 822, RFC 1123, RFC 850
 -- and ANSI C's asctime() formats. When the string can't be parsed, it
--- returns 'Nothing'.
+-- returns @'Left' err@.
 --
 -- This function is even more permissive than what HTTP\/1.1
 -- specifies. That is, it accepts 2-digit years in RFC 822, omitted
 -- separator symbols in RFC 850, omitted sec fields, and non-GMT time
 -- zones. I believe this behavior will not cause a problem but you
 -- should know this.
-parse :: String -> Maybe UTCTime
-parse src = case P.parse p "" src of
-              Right ut -> Just ut
-              Left  _  -> Nothing
+fromAscii ∷ Ascii → Either String UTCTime
+fromAscii = P.parseOnly p ∘ A.toByteString
     where
-      p = do zt <- rfc2616DateAndTime
-             _  <- P.eof
+      p = do zt ← httpDateAndTime
+             P.endOfInput
              return zt