X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Data%2FTime%2FRFC1123.hs;h=fb7839d034bbc9dc7aaae2369170f00e59a568f8;hb=2371481;hp=abb0a6d53e882eee4e70aaf27e3ea928a1cbce92;hpb=269a28dfe8b08af854f7217bbe9c141c9c18f1ec;p=time-http.git diff --git a/Data/Time/RFC1123.hs b/Data/Time/RFC1123.hs index abb0a6d..fb7839d 100644 --- a/Data/Time/RFC1123.hs +++ b/Data/Time/RFC1123.hs @@ -1,52 +1,43 @@ +{-# LANGUAGE + UnicodeSyntax + #-} +-- |This module provides functions to parse and format RFC 1123 date +-- and time formats. +-- +-- The format is basically same as RFC 822, but the syntax for @date@ +-- is changed from: +-- +-- > year ::= 2DIGIT +-- +-- to: +-- +-- > year ::= 4DIGIT module Data.Time.RFC1123 - ( format - , parse + ( -- * Formatting + toAscii + , toAsciiBuilder + + -- * Parsing + , fromAscii + , rfc1123DateAndTime ) 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.RFC1123.Parsec - -{- - The syntax for the date is hereby changed to: - - date = 1*2DIGIT month 2*4DIGIT --} +import Data.Time.RFC1123.Internal +import Prelude.Unicode -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 - , " " - , show4 year - , " " - , show2 (todHour timeOfDay) - , ":" - , show2 (todMin timeOfDay) - , ":" - , show2 (floor (todSec timeOfDay)) - , " " - , show4digitsTZ timeZone - ] +-- |Convert a 'ZonedTime' to RFC 1123 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 1123 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 <- rfc1123DateAndTime - _ <- P.eof + p = do zt ← rfc1123DateAndTime + P.endOfInput return zt