X-Git-Url: http://git.cielonegro.org/gitweb.cgi?p=time-http.git;a=blobdiff_plain;f=Data%2FTime%2FHTTP.hs;h=f106fc4c7f397f77466f75b3a986937ea68d16a3;hp=4352e8b0f02b2959964c9f56622e78896cc9113f;hb=98132c0;hpb=24bf3b8db05616dec2aa4e6d7445d985935005c4 diff --git a/Data/Time/HTTP.hs b/Data/Time/HTTP.hs index 4352e8b..f106fc4 100644 --- a/Data/Time/HTTP.hs +++ b/Data/Time/HTTP.hs @@ -1,3 +1,6 @@ +{-# LANGUAGE + UnicodeSyntax + #-} -- |This module provides functions to parse and format HTTP\/1.1 date -- and time formats. -- @@ -37,36 +40,38 @@ -- > | "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