module Data.Time.Format.RFC1123
( RFC1123
)
where
import Control.Applicative
import Control.Applicative.Unicode
import Data.Ascii (Ascii, AsciiBuilder)
import qualified Data.Ascii as A
import Data.Attoparsec.Char8
import Data.Convertible.Base
import Data.Default
import Data.Monoid.Unicode
import Data.Tagged
import Data.Time
import Data.Time.Calendar.WeekDate
import Data.Time.Format.HTTP.Common
import Data.Time.Format.RFC822
import Prelude.Unicode
data RFC1123
instance ConvertSuccess (Tagged RFC1123 ZonedTime) Ascii where
convertSuccess = A.fromAsciiBuilder ∘ cs
instance ConvertSuccess (Tagged RFC1123 ZonedTime) AsciiBuilder where
convertSuccess = toAsciiBuilder
instance ConvertAttempt Ascii (Tagged RFC1123 ZonedTime) where
convertAttempt = parseAttempt' def
instance Default (Parser (Tagged RFC1123 ZonedTime)) where
def = do weekDay ← optionMaybe $
do w ← shortWeekDayNameP
string ", " *> pure w
gregDay ← date
case weekDay of
Nothing
→ return ()
Just givenWD
→ assertWeekDayIsGood givenWD gregDay
tod ← def
tz ← char ' ' *> def
let lt = LocalTime gregDay <$> tod
zt = ZonedTime <$> lt ⊛ tz
pure $ retag' zt
where
retag' ∷ Tagged RFC822 α → Tagged τ α
retag' = retag
date ∷ Parser Day
date = do day ← read2
_ ← char ' '
month ← shortMonthNameP
_ ← char ' '
year ← read4
_ ← char ' '
assertGregorianDateIsGood year month day
toAsciiBuilder ∷ Tagged RFC1123 ZonedTime → AsciiBuilder
toAsciiBuilder zonedTime
= let localTime = zonedTimeToLocalTime $ untag zonedTime
timeZone = zonedTimeZone <$> retag' zonedTime
(year, month, day) = toGregorian (localDay localTime)
(_, _, week) = toWeekDate (localDay localTime)
timeOfDay = localTimeOfDay localTime
in
shortWeekDayName week
⊕ A.toAsciiBuilder ", "
⊕ show2 day
⊕ A.toAsciiBuilder " "
⊕ shortMonthName month
⊕ A.toAsciiBuilder " "
⊕ show4 year
⊕ A.toAsciiBuilder " "
⊕ show2 (todHour timeOfDay)
⊕ A.toAsciiBuilder ":"
⊕ show2 (todMin timeOfDay)
⊕ A.toAsciiBuilder ":"
⊕ show2 (floor (todSec timeOfDay) ∷ Int)
⊕ A.toAsciiBuilder " "
⊕ cs timeZone
where
retag' ∷ Tagged τ α → Tagged RFC822 α
retag' = retag
deriveAttempts [ ([t| Tagged RFC1123 ZonedTime |], [t| Ascii |])
, ([t| Tagged RFC1123 ZonedTime |], [t| AsciiBuilder |])
]