X-Git-Url: http://git.cielonegro.org/gitweb.cgi?p=time-http.git;a=blobdiff_plain;f=Data%2FTime%2FRFC822.hs;h=152d99290f57e89915e72eaf80efd36e72c22e1e;hp=5d4b862c760aadb1508e7e448a5cb85242e45a7d;hb=d82d61b;hpb=512f9a871149c7dd20d0c1c86cb230fbb7dc43f6 diff --git a/Data/Time/RFC822.hs b/Data/Time/RFC822.hs index 5d4b862..152d992 100644 --- a/Data/Time/RFC822.hs +++ b/Data/Time/RFC822.hs @@ -1,3 +1,6 @@ +{-# LANGUAGE + UnicodeSyntax + #-} -- |This module provides functions to parse and format RFC 822 date -- and time formats. -- @@ -28,57 +31,31 @@ -- > | "Y" ; +12 -- > | ("+" | "-") 4DIGIT ; Local diff: HHMM module Data.Time.RFC822 - ( format - , parse + ( -- * Formatting + toAscii + , toAsciiBuilder - -- private - , showRFC822TimeZone + -- * Parsing + , fromAscii + , rfc822DateAndTime ) where +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.Internal +import Prelude.Unicode --- |Format a 'ZonedTime' in RFC 822. -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)) - , " " - , showRFC822TimeZone timeZone - ] - --- private -showRFC822TimeZone :: TimeZone -> String -showRFC822TimeZone tz - | timeZoneMinutes tz == 0 = "GMT" - | otherwise = show4digitsTZ tz +-- |Convert a 'ZonedTime' to RFC 822 date and time string. +toAscii ∷ ZonedTime → Ascii +toAscii = A.fromAsciiBuilder ∘ toAsciiBuilder -- |Parse an RFC 822 date and time string. When the string can't be --- parsed, it returns 'Nothing'. -parse :: String -> Maybe ZonedTime -parse src = case P.parse p "" src of - Right zt -> Just zt - Left _ -> Nothing +-- parsed, it returns @'Left' err@. +fromAscii ∷ Ascii → Either String ZonedTime +fromAscii = P.parseOnly p ∘ A.toByteString where - p = do zt <- rfc822DateAndTime - _ <- P.eof + p = do zt ← rfc822DateAndTime + P.endOfInput return zt