graphql-engine/server/src-lib/Hasura/SQL/Time.hs

32 lines
858 B
Haskell
Raw Normal View History

2018-06-27 16:11:32 +03:00
module Hasura.SQL.Time
( ZonedTimeOfDay (..),
)
where
2018-06-27 16:11:32 +03:00
import Data.Aeson.Types qualified as Aeson
import Data.Attoparsec.Text as A
import Data.Attoparsec.Time (timeOfDay, timeZone)
import Data.Time.LocalTime qualified as Local
import Hasura.Prelude
2018-06-27 16:11:32 +03:00
data ZonedTimeOfDay = ZonedTimeOfDay
{ ztodTime :: Local.TimeOfDay,
ztodZone :: Local.TimeZone
}
deriving (Show, Eq)
2018-06-27 16:11:32 +03:00
utc :: Local.TimeZone
utc = Local.TimeZone 0 False ""
zonedTimeOfDay :: Text -> Aeson.Parser ZonedTimeOfDay
2018-06-27 16:11:32 +03:00
zonedTimeOfDay t =
case A.parseOnly (p <* endOfInput) t of
Left err -> fail $ "could not parse timetz: " ++ err
Right r -> return r
2018-06-27 16:11:32 +03:00
where
p = ZonedTimeOfDay <$> timeOfDay <*> (fromMaybe utc <$> timeZone)
instance Aeson.FromJSON ZonedTimeOfDay where
parseJSON (Aeson.String t) = zonedTimeOfDay t
parseJSON _ = fail "Expecting a string for timetz"