]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Utils/UTCTime.hs
Merge branch 'dev' into 131-dev-ngrams-table-db-connection-2
[gargantext.git] / src / Gargantext / Utils / UTCTime.hs
1 {-# LANGUAGE DeriveAnyClass #-}
2 {-# LANGUAGE DeriveGeneric #-}
3 {-# LANGUAGE TypeFamilies #-}
4
5 module Gargantext.Utils.UTCTime where
6
7 import Data.Aeson (FromJSON, ToJSON)
8 import Data.Either (Either(..))
9 import Data.Morpheus.Kind (SCALAR)
10 import Data.Morpheus.Types (GQLType(..), DecodeScalar(..), EncodeScalar(..))
11 import qualified Data.Morpheus.Types as DMT
12 import Data.Swagger (ToSchema)
13 import qualified Data.Text as T
14 import Data.Time (UTCTime)
15 import Gargantext.Prelude
16 import GHC.Generics (Generic)
17 import Text.Read (readEither)
18
19
20 newtype NUTCTime = NUTCTime UTCTime
21 deriving (Eq, Show, Generic)
22 instance DecodeScalar NUTCTime where
23 decodeScalar (DMT.String x) = case (readEither $ T.unpack x) of
24 Right r -> pure $ NUTCTime r
25 Left err -> Left $ T.pack err
26 decodeScalar _ = Left "Invalid value for NUTCTime"
27 instance EncodeScalar NUTCTime where
28 encodeScalar (NUTCTime x) = DMT.String $ T.pack $ show x
29 instance GQLType NUTCTime where
30 type KIND NUTCTime = SCALAR
31 instance FromJSON NUTCTime
32 instance ToJSON NUTCTime
33 instance ToSchema NUTCTime