git.iko.soy/typeable/postgresql-simple
on master help sign in
add FromField/ToField instance declarations for ZonedTime

Minor tweak of 94ff9e4fbd1c0b481db04706b1d0529721f7da21 See https://github.com/lpsmith/postgresql-simple/pull/19

commit44de07093070eb0cc6e8da6054dfe8cabd0ef122 authorrekado <rekado@elephly.net> dateTue 29 May 2012, 18:31 (14 years ago) parent9268827
2 files changed+19−2
src/Database/PostgreSQL/Simple/FromField.hs+14−1 src/Database/PostgreSQL/Simple/ToField.hs+5−1
src/Database/PostgreSQL/Simple/FromField.hs+14−1
@@ -54,7 +54,7 @@ import Data.Ratio (Ratio)
5454import Data.Time.Calendar (Day, fromGregorian)
5555import Data.Time.Clock (UTCTime)
5656import Data.Time.Format (parseTime)
57−import Data.Time.LocalTime (TimeOfDay, makeTimeOfDayValid)
57+import Data.Time.LocalTime (ZonedTime, TimeOfDay, makeTimeOfDayValid)
5858import Data.Typeable (Typeable, typeOf)
5959import Data.Word (Word64)
6060import Database.PostgreSQL.Simple.Internal
@@ -209,6 +209,19 @@ instance FromField UTCTime where
209209 Nothing -> returnError ConversionFailed f "could not parse"
210210 where str = preprocess (B8.unpack bs)
211211
212+instance FromField ZonedTime where
213+ fromField f =
214+ case oid2builtin (typeOid f) of
215+ Just TimestampWithTimeZone -> doIt "%F %T%Q%z" (++ "00")
216+ _ -> const $ returnError Incompatible f "types incompatible"
217+ where
218+ doIt _ _ Nothing = returnError UnexpectedNull f ""
219+ doIt fmt preprocess (Just bs) =
220+ case parseTime defaultTimeLocale fmt str of
221+ Just t -> pure t
222+ Nothing -> returnError ConversionFailed f "could not parse"
223+ where str = preprocess (B8.unpack bs)
224+
212225instance FromField Day where
213226 fromField f = atto ok date f
214227 where ok = mkCompats [Date]
src/Database/PostgreSQL/Simple/ToField.hs+5−1
@@ -31,7 +31,7 @@ import Data.Monoid (mappend)
3131import Data.Time.Calendar (Day, showGregorian)
3232import Data.Time.Clock (UTCTime)
3333import Data.Time.Format (formatTime)
34−import Data.Time.LocalTime (TimeOfDay)
34+import Data.Time.LocalTime (TimeOfDay, ZonedTime)
3535import Data.Typeable (Typeable)
3636import Data.Word (Word, Word8, Word16, Word32, Word64)
3737import Database.PostgreSQL.Simple.Types (Binary(..), In(..), Null)
@@ -191,6 +191,10 @@ instance ToField UTCTime where
191191 toField = Plain . Utf8.fromString . formatTime defaultTimeLocale "'%F %T%Q+00'"
192192 {-# INLINE toField #-}
193193
194+instance ToField ZonedTime where
195+ toField = Plain . Utf8.fromString . formatTime defaultTimeLocale "'%F %T%Q%z'"
196+ {-# INLINE toField #-}
197+
194198instance ToField Day where
195199 toField = Plain . inQuotes . Utf8.fromString . showGregorian
196200 {-# INLINE toField #-}