{-# LANGUAGE NoImplicitPrelude #-}
module Data.Aeson.Parser.Time
(
run
, day
, localTime
, timeOfDay
, timeZone
, utcTime
, zonedTime
) where
import Prelude.Compat
import Data.Attoparsec.Text (Parser)
import Data.Text (Text)
import Data.Time.Calendar (Day)
import Data.Time.Clock (UTCTime(..))
import qualified Data.Aeson.Types.Internal as Aeson
import qualified Data.Attoparsec.Text as A
import qualified Data.Attoparsec.Time as T
import qualified Data.Time.LocalTime as Local
run :: Parser a -> Text -> Aeson.Parser a
run p t = case A.parseOnly (p <* A.endOfInput) t of
Left err -> fail $ "could not parse date: " ++ err
Right r -> return r
day :: Parser Day
day = T.day
{-# INLINE day #-}
timeOfDay :: Parser Local.TimeOfDay
timeOfDay = T.timeOfDay
{-# INLINE timeOfDay #-}
timeZone :: Parser (Maybe Local.TimeZone)
timeZone = T.timeZone
{-# INLINE timeZone #-}
localTime :: Parser Local.LocalTime
localTime = T.localTime
{-# INLINE localTime #-}
utcTime :: Parser UTCTime
utcTime = T.utcTime
{-# INLINE utcTime #-}
zonedTime :: Parser Local.ZonedTime
zonedTime = T.zonedTime
{-# INLINE zonedTime #-}