Skip to content

Commit

Permalink
Change DB representation of POSIXTime
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Ospald committed Oct 17, 2020
1 parent 1f5eb7d commit 3ec8ab9
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/core/src/Cardano/Wallet/DB/Sqlite/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ import Data.Text.Encoding
( decodeUtf8, encodeUtf8 )
import Data.Time.Clock.POSIX
( POSIXTime, posixSecondsToUTCTime, utcTimeToPOSIXSeconds )
import Data.Time.Format
( defaultTimeLocale, formatTime, iso8601DateFormat, parseTimeM )
import Data.Word
( Word32, Word64 )
import Data.Word.Odd
Expand Down Expand Up @@ -665,9 +667,16 @@ instance PersistFieldSql DerivationPrefix where
-- Other

instance PersistField POSIXTime where
toPersistValue = PersistUTCTime . posixSecondsToUTCTime
fromPersistValue (PersistUTCTime utc) = Right . utcTimeToPOSIXSeconds $ utc
fromPersistValue _ = Left "Could not convert to unknown construtctor POSIX seconds"
toPersistValue =
PersistText
. T.pack
. formatTime defaultTimeLocale (iso8601DateFormat (Just "%H:%M:%S"))
. posixSecondsToUTCTime
fromPersistValue (PersistText time) =
fmap utcTimeToPOSIXSeconds $
parseTimeM True defaultTimeLocale
(iso8601DateFormat (Just "%H:%M:%S")) (T.unpack time)
fromPersistValue _ = Left "Could not convert to unknown constructor POSIX seconds"

instance PersistFieldSql POSIXTime where
sqlType _ = sqlType (Proxy @Word64)
sqlType _ = sqlType (Proxy @Text)

0 comments on commit 3ec8ab9

Please sign in to comment.