module Data.Time.Format.C
( C
)
where
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 Prelude.Unicode
data C
instance ConvertSuccess (Tagged C LocalTime) Ascii where
convertSuccess = A.fromAsciiBuilder ∘ cs
instance ConvertSuccess (Tagged C LocalTime) AsciiBuilder where
convertSuccess = toAsciiBuilder ∘ untag
instance ConvertAttempt Ascii (Tagged C LocalTime) where
convertAttempt = parseAttempt' def
instance Default (Parser (Tagged C LocalTime)) where
def = do weekDay ← shortWeekDayNameP
_ ← char ' '
month ← shortMonthNameP
_ ← char ' '
day ← read2'
_ ← char ' '
hour ← read2
_ ← char ':'
minute ← read2
_ ← char ':'
second ← read2
_ ← char ' '
year ← read4
gregDay ← assertGregorianDateIsGood year month day
_ ← assertWeekDayIsGood weekDay gregDay
tod ← assertTimeOfDayIsGood hour minute second
return ∘ Tagged $ LocalTime gregDay tod
toAsciiBuilder ∷ LocalTime → AsciiBuilder
toAsciiBuilder localTime
= let (year, month, day) = toGregorian (localDay localTime)
(_, _, week) = toWeekDate (localDay localTime)
timeOfDay = localTimeOfDay localTime
in
shortWeekDayName week
⊕ A.toAsciiBuilder " "
⊕ shortMonthName month
⊕ A.toAsciiBuilder " "
⊕ show2' day
⊕ A.toAsciiBuilder " "
⊕ show2 (todHour timeOfDay)
⊕ A.toAsciiBuilder ":"
⊕ show2 (todMin timeOfDay)
⊕ A.toAsciiBuilder ":"
⊕ show2 (floor (todSec timeOfDay) ∷ Int)
⊕ A.toAsciiBuilder " "
⊕ show4 year
deriveAttempts [ ([t| Tagged C LocalTime |], [t| Ascii |])
, ([t| Tagged C LocalTime |], [t| AsciiBuilder |])
]