2018-06-27 16:11:32 +03:00
|
|
|
module Hasura.SQL.Time
|
2021-09-24 01:56:37 +03:00
|
|
|
( ZonedTimeOfDay (..),
|
|
|
|
)
|
|
|
|
where
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-09-24 01:56:37 +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
|
|
|
|
2021-09-24 01:56:37 +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 ""
|
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
zonedTimeOfDay :: Text -> Aeson.Parser ZonedTimeOfDay
|
2018-06-27 16:11:32 +03:00
|
|
|
zonedTimeOfDay t =
|
2022-06-21 14:11:08 +03:00
|
|
|
onLeft
|
|
|
|
(A.parseOnly (p <* endOfInput) t)
|
|
|
|
(\err -> fail $ "could not parse timetz: " ++ err)
|
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
|
2021-09-24 01:56:37 +03:00
|
|
|
parseJSON _ = fail "Expecting a string for timetz"
|