module Data.Hermes.Decoder.Time
( day
, localTime
, month
, quarter
, timeOfDay
, timeZone
, utcTime
, zonedTime
) where
import qualified Data.Time.FromText as Time
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.Internal (Decoder(..))
import Data.Hermes.Decoder.Value (withText)
runParser :: (Text -> Either String a) -> Text -> Decoder a
runParser :: forall a. (Text -> Either String a) -> Text -> Decoder a
runParser Text -> Either String a
p Text
t =
case Text -> Either String a
p 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. Semigroup a => a -> a -> a
<> String
err
Right a
r -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
r
{-# INLINE runParser #-}
day :: Decoder Time.Day
day :: Decoder Day
day = forall a. (Text -> Decoder a) -> Decoder a
withText forall a b. (a -> b) -> a -> b
$ forall a. (Text -> Either String a) -> Text -> Decoder a
runParser Text -> Either String Day
Time.parseDay
month :: Decoder Time.Month
month :: Decoder Month
month = forall a. (Text -> Decoder a) -> Decoder a
withText forall a b. (a -> b) -> a -> b
$ forall a. (Text -> Either String a) -> Text -> Decoder a
runParser Text -> Either String Month
Time.parseMonth
quarter :: Decoder Time.Quarter
quarter :: Decoder Quarter
quarter = forall a. (Text -> Decoder a) -> Decoder a
withText forall a b. (a -> b) -> a -> b
$ forall a. (Text -> Either String a) -> Text -> Decoder a
runParser Text -> Either String Quarter
Time.parseQuarter
timeOfDay :: Decoder Local.TimeOfDay
timeOfDay :: Decoder TimeOfDay
timeOfDay = forall a. (Text -> Decoder a) -> Decoder a
withText forall a b. (a -> b) -> a -> b
$ forall a. (Text -> Either String a) -> Text -> Decoder a
runParser Text -> Either String TimeOfDay
Time.parseTimeOfDay
timeZone :: Decoder Local.TimeZone
timeZone :: Decoder TimeZone
timeZone = forall a. (Text -> Decoder a) -> Decoder a
withText forall a b. (a -> b) -> a -> b
$ forall a. (Text -> Either String a) -> Text -> Decoder a
runParser Text -> Either String TimeZone
Time.parseTimeZone
localTime :: Decoder Local.LocalTime
localTime :: Decoder LocalTime
localTime = forall a. (Text -> Decoder a) -> Decoder a
withText forall a b. (a -> b) -> a -> b
$ forall a. (Text -> Either String a) -> Text -> Decoder a
runParser Text -> Either String LocalTime
Time.parseLocalTime
utcTime :: Decoder Time.UTCTime
utcTime :: Decoder UTCTime
utcTime = forall a. (Text -> Decoder a) -> Decoder a
withText forall a b. (a -> b) -> a -> b
$ forall a. (Text -> Either String a) -> Text -> Decoder a
runParser Text -> Either String UTCTime
Time.parseUTCTime
zonedTime :: Decoder Local.ZonedTime
zonedTime :: Decoder ZonedTime
zonedTime = forall a. (Text -> Decoder a) -> Decoder a
withText forall a b. (a -> b) -> a -> b
$ forall a. (Text -> Either String a) -> Text -> Decoder a
runParser Text -> Either String ZonedTime
Time.parseZonedTime