{-# LANGUAGE
    UnicodeSyntax
  #-}
-- |Internal functions for "Data.Time.HTTP".
module Data.Time.HTTP.Internal
    ( httpDateAndTime
    , toAsciiBuilder
    )
    where
import Control.Applicative
import Data.Ascii (AsciiBuilder)
import Data.Attoparsec.Char8
import Data.Time
import qualified Data.Time.RFC1123.Internal as RFC1123
import qualified Data.Time.RFC733.Internal  as RFC733
import qualified Data.Time.Asctime.Internal as Asctime
import Prelude.Unicode

-- |Parse a date and time string in any formats allowed by HTTP\/1.1
-- (RFC 2616).
httpDateAndTime  Parser UTCTime
httpDateAndTime
    = choice [ zonedTimeToUTC     <$> try RFC1123.rfc1123DateAndTime
             , zonedTimeToUTC     <$> try RFC733.rfc733DateAndTime
             , localTimeToUTC utc <$> Asctime.asctime
             ]

-- |Convert a 'UTCTime' to RFC 1123 date and time string.
toAsciiBuilder  UTCTime  AsciiBuilder
toAsciiBuilder = RFC1123.toAsciiBuilder  ut2zt
    where
      ut2zt  UTCTime  ZonedTime
      ut2zt = utcToZonedTime gmt

      gmt  TimeZone
      gmt = TimeZone 0 False "GMT"