module Data.Hermes.Decoder.Time
( day
, localTime
, month
, quarter
, timeOfDay
, timeZone
, utcTime
, zonedTime
) where
import qualified Data.Attoparsec.Text as AT
import qualified Data.Attoparsec.Time as ATime
import Data.Text (Text)
import qualified Data.Time as Time
import qualified Data.Time.Calendar.Month.Compat as Time
import qualified Data.Time.Calendar.Quarter.Compat as Time
import qualified Data.Time.LocalTime as Local
import Data.Hermes.Decoder.Types (Decoder)
import Data.Hermes.Decoder.Value (withText)
import Data.Hermes.SIMDJSON
runAttoDate :: AT.Parser a -> Text -> Decoder a
runAttoDate :: Parser a -> Text -> Decoder a
runAttoDate Parser a
p Text
t =
case Parser a -> Text -> Either String a
forall a. Parser a -> Text -> Either String a
AT.parseOnly (Parser a
p Parser a -> Parser Text () -> Parser a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser Text ()
forall t. Chunk t => Parser t ()
AT.endOfInput) Text
t of
Left String
err -> String -> Decoder a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder a) -> String -> Decoder a
forall a b. (a -> b) -> a -> b
$ String
"Could not parse date: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
err
Right a
r -> a -> Decoder a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
r
{-# INLINE runAttoDate #-}
day :: Value -> Decoder Time.Day
day :: Value -> Decoder Day
day = (Text -> Decoder Day) -> Value -> Decoder Day
forall a. (Text -> Decoder a) -> Value -> Decoder a
withText ((Text -> Decoder Day) -> Value -> Decoder Day)
-> (Text -> Decoder Day) -> Value -> Decoder Day
forall a b. (a -> b) -> a -> b
$ Parser Day -> Text -> Decoder Day
forall a. Parser a -> Text -> Decoder a
runAttoDate Parser Day
ATime.day
month :: Value -> Decoder Time.Month
month :: Value -> Decoder Month
month = (Text -> Decoder Month) -> Value -> Decoder Month
forall a. (Text -> Decoder a) -> Value -> Decoder a
withText ((Text -> Decoder Month) -> Value -> Decoder Month)
-> (Text -> Decoder Month) -> Value -> Decoder Month
forall a b. (a -> b) -> a -> b
$ Parser Month -> Text -> Decoder Month
forall a. Parser a -> Text -> Decoder a
runAttoDate Parser Month
ATime.month
quarter :: Value -> Decoder Time.Quarter
quarter :: Value -> Decoder Quarter
quarter = (Text -> Decoder Quarter) -> Value -> Decoder Quarter
forall a. (Text -> Decoder a) -> Value -> Decoder a
withText ((Text -> Decoder Quarter) -> Value -> Decoder Quarter)
-> (Text -> Decoder Quarter) -> Value -> Decoder Quarter
forall a b. (a -> b) -> a -> b
$ Parser Quarter -> Text -> Decoder Quarter
forall a. Parser a -> Text -> Decoder a
runAttoDate Parser Quarter
ATime.quarter
timeOfDay :: Value -> Decoder Local.TimeOfDay
timeOfDay :: Value -> Decoder TimeOfDay
timeOfDay = (Text -> Decoder TimeOfDay) -> Value -> Decoder TimeOfDay
forall a. (Text -> Decoder a) -> Value -> Decoder a
withText ((Text -> Decoder TimeOfDay) -> Value -> Decoder TimeOfDay)
-> (Text -> Decoder TimeOfDay) -> Value -> Decoder TimeOfDay
forall a b. (a -> b) -> a -> b
$ Parser TimeOfDay -> Text -> Decoder TimeOfDay
forall a. Parser a -> Text -> Decoder a
runAttoDate Parser TimeOfDay
ATime.timeOfDay
timeZone :: Value -> Decoder (Maybe Local.TimeZone)
timeZone :: Value -> Decoder (Maybe TimeZone)
timeZone = (Text -> Decoder (Maybe TimeZone))
-> Value -> Decoder (Maybe TimeZone)
forall a. (Text -> Decoder a) -> Value -> Decoder a
withText ((Text -> Decoder (Maybe TimeZone))
-> Value -> Decoder (Maybe TimeZone))
-> (Text -> Decoder (Maybe TimeZone))
-> Value
-> Decoder (Maybe TimeZone)
forall a b. (a -> b) -> a -> b
$ Parser (Maybe TimeZone) -> Text -> Decoder (Maybe TimeZone)
forall a. Parser a -> Text -> Decoder a
runAttoDate Parser (Maybe TimeZone)
ATime.timeZone
localTime :: Value -> Decoder Local.LocalTime
localTime :: Value -> Decoder LocalTime
localTime = (Text -> Decoder LocalTime) -> Value -> Decoder LocalTime
forall a. (Text -> Decoder a) -> Value -> Decoder a
withText ((Text -> Decoder LocalTime) -> Value -> Decoder LocalTime)
-> (Text -> Decoder LocalTime) -> Value -> Decoder LocalTime
forall a b. (a -> b) -> a -> b
$ Parser LocalTime -> Text -> Decoder LocalTime
forall a. Parser a -> Text -> Decoder a
runAttoDate Parser LocalTime
ATime.localTime
utcTime :: Value -> Decoder Time.UTCTime
utcTime :: Value -> Decoder UTCTime
utcTime = (Text -> Decoder UTCTime) -> Value -> Decoder UTCTime
forall a. (Text -> Decoder a) -> Value -> Decoder a
withText ((Text -> Decoder UTCTime) -> Value -> Decoder UTCTime)
-> (Text -> Decoder UTCTime) -> Value -> Decoder UTCTime
forall a b. (a -> b) -> a -> b
$ Parser UTCTime -> Text -> Decoder UTCTime
forall a. Parser a -> Text -> Decoder a
runAttoDate Parser UTCTime
ATime.utcTime
zonedTime :: Value -> Decoder Local.ZonedTime
zonedTime :: Value -> Decoder ZonedTime
zonedTime = (Text -> Decoder ZonedTime) -> Value -> Decoder ZonedTime
forall a. (Text -> Decoder a) -> Value -> Decoder a
withText ((Text -> Decoder ZonedTime) -> Value -> Decoder ZonedTime)
-> (Text -> Decoder ZonedTime) -> Value -> Decoder ZonedTime
forall a b. (a -> b) -> a -> b
$ Parser ZonedTime -> Text -> Decoder ZonedTime
forall a. Parser a -> Text -> Decoder a
runAttoDate Parser ZonedTime
ATime.zonedTime