git.iko.soy/typeable/postgresql-simple
on master help sign in
Add ToField instance for NominalDiffTime
commit46a6b3aabdbd1d244e2a4935fb231e70fb27a757 authorLeon P Smith <leon@melding-monads.com> dateTue 7 Oct 2014, 19:27 (12 years ago) parent435df29
3 files changed+18−1
src/Database/PostgreSQL/Simple/Time.hs+1 src/Database/PostgreSQL/Simple/Time/Implementation.hs+11 src/Database/PostgreSQL/Simple/ToField.hs+6−1
src/Database/PostgreSQL/Simple/Time.hs+1
@@ -75,6 +75,7 @@ module Database.PostgreSQL.Simple.Time
7575 , zonedTimestampToBuilder
7676 , localTimestampToBuilder
7777 , unboundedToBuilder
78+ , nominalDiffTimeToBuilder
7879 ) where
7980
8081import Database.PostgreSQL.Simple.Time.Implementation
src/Database/PostgreSQL/Simple/Time/Implementation.hs+11
@@ -229,6 +229,17 @@ localTimestampToBuilder = unboundedToBuilder localTimeToBuilder
229229dateToBuilder :: Date -> Builder
230230dateToBuilder = unboundedToBuilder dayToBuilder
231231
232+nominalDiffTimeToBuilder :: NominalDiffTime -> Builder
233+nominalDiffTimeToBuilder xyz
234+ | yz == 0 = integral x
235+ | z == 0 = integral x ++ fromChar '.' ++ showD6 y
236+ | otherwise = integral x ++ fromChar '.' ++ pad6 y ++ showD6 z
237+ where
238+ -- A kludge to work around the fact that Data.Fixed isn't very fast and
239+ -- doesn't give me access to the MkFixed constructor.
240+ (x,yz) = (unsafeCoerce xyz :: Integer) `quotRem` 1000000000000
241+ (fromIntegral -> y, fromIntegral -> z) = yz `quotRem` 1000000
242+
232243showSeconds :: Pico -> Builder
233244showSeconds xyz
234245 | yz == 0 = pad2 x
src/Database/PostgreSQL/Simple/ToField.hs+6−1
@@ -30,7 +30,7 @@ import Data.ByteString (ByteString)
3030import Data.Int (Int8, Int16, Int32, Int64)
3131import Data.List (intersperse)
3232import Data.Monoid (mappend)
33−import Data.Time (Day, TimeOfDay, LocalTime, UTCTime, ZonedTime)
33+import Data.Time (Day, TimeOfDay, LocalTime, UTCTime, ZonedTime, NominalDiffTime)
3434import Data.Typeable (Typeable)
3535import Data.Word (Word, Word8, Word16, Word32, Word64)
3636import {-# SOURCE #-} Database.PostgreSQL.Simple.ToRow
@@ -225,6 +225,7 @@ instance ToField LT.Text where
225225 toField = toField . LT.toStrict
226226 {-# INLINE toField #-}
227227
228+
228229instance ToField UTCTime where
229230 toField = Plain . inQuotes . utcTimeToBuilder
230231 {-# INLINE toField #-}
@@ -261,6 +262,10 @@ instance ToField Date where
261262 toField = Plain . inQuotes . dateToBuilder
262263 {-# INLINE toField #-}
263264
265+instance ToField NominalDiffTime where
266+ toField = Plain . inQuotes . nominalDiffTimeToBuilder
267+ {-# INLINE toField #-}
268+
264269instance (ToField a) => ToField (PGArray a) where
265270 toField xs = Many $
266271 Plain (fromByteString "ARRAY[") :