{-# 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 Parser a -> Text -> Either String a
forall a. Parser a -> Text -> Either String a
A.parseOnly (Parser a
p Parser a -> Parser Text () -> Parser a
forall a b. Parser Text a -> Parser Text b -> Parser Text a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser Text ()
forall t. Chunk t => Parser t ()
A.endOfInput) Text
t of
Left String
err -> String -> Parser a
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser a) -> String -> Parser a
forall a b. (a -> b) -> a -> b
$ String
"could not parse date: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
err
Right a
r -> a -> Parser a
forall a. a -> Parser a
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 #-}