From de1ee78b5560a172464614f712ae6719b585e174 Mon Sep 17 00:00:00 2001 From: Leif Warner Date: Tue, 10 Jul 2018 12:09:41 -0700 Subject: [PATCH] Use Data.Time.Clock.POSIX.utcTimeToPOSIXSecond for UTCTime Instead of our own code for converting time to an Integer. --- Aws/DynamoDb/Core.hs | 41 ++++------------------------------------- 1 file changed, 4 insertions(+), 37 deletions(-) diff --git a/Aws/DynamoDb/Core.hs b/Aws/DynamoDb/Core.hs index bfb8bec1..2d78056c 100644 --- a/Aws/DynamoDb/Core.hs +++ b/Aws/DynamoDb/Core.hs @@ -154,6 +154,7 @@ import Data.Tagged import qualified Data.Text as T import qualified Data.Text.Encoding as T import Data.Time +import Data.Time.Clock.POSIX import Data.Typeable import qualified Data.Vector as V import Data.Word @@ -392,45 +393,11 @@ instance DynVal Day where ------------------------------------------------------------------------------- --- | Losslessly encoded via 'Integer' picoseconds +-- | Losslessly encoded via 'Scientific' picoseconds instance DynVal UTCTime where type DynRep UTCTime = DynNumber - fromRep num = fromTS <$> fromRep num - toRep x = toRep (toTS x) - - -------------------------------------------------------------------------------- -pico :: Rational -pico = toRational $ (10 :: Integer) ^ (12 :: Integer) - - -------------------------------------------------------------------------------- -dayPico :: Integer -dayPico = 86400 * round pico - - -------------------------------------------------------------------------------- --- | Convert UTCTime to picoseconds --- --- TODO: Optimize performance? -toTS :: UTCTime -> Integer -toTS (UTCTime (ModifiedJulianDay i) diff) = i' + diff' - where - diff' = floor (toRational diff * pico) - i' = i * dayPico - - -------------------------------------------------------------------------------- --- | Convert picoseconds to UTCTime --- --- TODO: Optimize performance? -fromTS :: Integer -> UTCTime -fromTS i = UTCTime (ModifiedJulianDay days) diff - where - (days, secs) = i `divMod` dayPico - diff = fromRational ((toRational secs) / pico) - - + fromRep (DynNumber i) = Just $ posixSecondsToUTCTime $ realToFrac i + toRep = DynNumber . realToFrac . utcTimeToPOSIXSeconds -- | Type wrapper for binary data to be written to DynamoDB. Wrap any -- 'Serialize' instance in there and 'DynVal' will know how to