git.iko.soy/typeable/postgresql-simple
on master help sign in
Add ToField instance for NominalDiffTime
46a6b3aLeon P Smith
src/Database/PostgreSQL/Simple/Time.hs+1
@@ -75,6 +75,7 @@ module Database.PostgreSQL.Simple.Time
75 , zonedTimestampToBuilder
75 , zonedTimestampToBuilder
76 , localTimestampToBuilder
76 , localTimestampToBuilder
77 , unboundedToBuilder
77 , unboundedToBuilder
78+ , nominalDiffTimeToBuilder
78 ) where
79 ) where
79
80
80import Database.PostgreSQL.Simple.Time.Implementation
81import Database.PostgreSQL.Simple.Time.Implementation
src/Database/PostgreSQL/Simple/Time/Implementation.hs+11
@@ -229,6 +229,17 @@ localTimestampToBuilder = unboundedToBuilder localTimeToBuilder
229dateToBuilder :: Date -> Builder
229dateToBuilder :: Date -> Builder
230dateToBuilder = unboundedToBuilder dayToBuilder
230dateToBuilder = unboundedToBuilder dayToBuilder
231
231
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+
232showSeconds :: Pico -> Builder
243showSeconds :: Pico -> Builder
233showSeconds xyz
244showSeconds xyz
234 | yz == 0 = pad2 x
245 | yz == 0 = pad2 x
src/Database/PostgreSQL/Simple/ToField.hs+6−1
@@ -30,7 +30,7 @@ import Data.ByteString (ByteString)
30import Data.Int (Int8, Int16, Int32, Int64)
30import Data.Int (Int8, Int16, Int32, Int64)
31import Data.List (intersperse)
31import Data.List (intersperse)
32import Data.Monoid (mappend)
32import Data.Monoid (mappend)
33−import Data.Time (Day, TimeOfDay, LocalTime, UTCTime, ZonedTime)
33+import Data.Time (Day, TimeOfDay, LocalTime, UTCTime, ZonedTime, NominalDiffTime)
34import Data.Typeable (Typeable)
34import Data.Typeable (Typeable)
35import Data.Word (Word, Word8, Word16, Word32, Word64)
35import Data.Word (Word, Word8, Word16, Word32, Word64)
36import {-# SOURCE #-} Database.PostgreSQL.Simple.ToRow
36import {-# SOURCE #-} Database.PostgreSQL.Simple.ToRow
@@ -225,6 +225,7 @@ instance ToField LT.Text where
225 toField = toField . LT.toStrict
225 toField = toField . LT.toStrict
226 {-# INLINE toField #-}
226 {-# INLINE toField #-}
227
227
228+
228instance ToField UTCTime where
229instance ToField UTCTime where
229 toField = Plain . inQuotes . utcTimeToBuilder
230 toField = Plain . inQuotes . utcTimeToBuilder
230 {-# INLINE toField #-}
231 {-# INLINE toField #-}
@@ -261,6 +262,10 @@ instance ToField Date where
261 toField = Plain . inQuotes . dateToBuilder
262 toField = Plain . inQuotes . dateToBuilder
262 {-# INLINE toField #-}
263 {-# INLINE toField #-}
263
264
265+instance ToField NominalDiffTime where
266+ toField = Plain . inQuotes . nominalDiffTimeToBuilder
267+ {-# INLINE toField #-}
268+
264instance (ToField a) => ToField (PGArray a) where
269instance (ToField a) => ToField (PGArray a) where
265 toField xs = Many $
270 toField xs = Many $
266 Plain (fromByteString "ARRAY[") :
271 Plain (fromByteString "ARRAY[") :