{-# LANGUAGE NoImplicitPrelude #-}
module Data.Aeson.Parser.Time
(
run
, day
, month
, quarter
, 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.Calendar.Quarter.Compat (Quarter)
import Data.Time.Calendar.Month.Compat (Month)
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 :: forall a. Parser a -> Text -> Parser a
run Parser a
p Text
t = case forall a. Parser a -> Text -> Either String a
A.parseOnly (Parser a
p forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall t. Chunk t => Parser t ()
A.endOfInput) Text
t of
Left String
err -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ String
"could not parse date: " forall a. [a] -> [a] -> [a]
++ String
err
Right a
r -> forall (m :: * -> *) a. Monad m => a -> m a
return a
r
day :: Parser Day
day :: Parser Day
day = Parser Day
T.day
{-# INLINE day #-}
month :: Parser Month
month :: Parser Month
month = Parser Month
T.month
{-# INLINE month #-}
quarter :: Parser Quarter
quarter :: Parser Quarter
quarter = Parser Quarter
T.quarter
{-# INLINE quarter #-}
timeOfDay :: Parser Local.TimeOfDay
timeOfDay :: Parser TimeOfDay
timeOfDay = Parser TimeOfDay
T.timeOfDay
{-# INLINE timeOfDay #-}
timeZone :: Parser (Maybe Local.TimeZone)
timeZone :: Parser (Maybe TimeZone)
timeZone = Parser (Maybe TimeZone)
T.timeZone
{-# INLINE timeZone #-}
localTime :: Parser Local.LocalTime
localTime :: Parser LocalTime
localTime = Parser LocalTime
T.localTime
{-# INLINE localTime #-}
utcTime :: Parser UTCTime
utcTime :: Parser UTCTime
utcTime = Parser UTCTime
T.utcTime
{-# INLINE utcTime #-}
zonedTime :: Parser Local.ZonedTime
zonedTime :: Parser ZonedTime
zonedTime = Parser ZonedTime
T.zonedTime
{-# INLINE zonedTime #-}