Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

add FromField/ToField instance declarations for ZonedTime #19

Closed
wants to merge 1 commit into from

2 participants

@rekado

No description provided.

@lpsmith
Owner

Ok, I committed this as 44de070 and released postgresql-simple-0.1.3 on hackage incorporating these changes.

@lpsmith lpsmith closed this
@rekado

Nice, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 29, 2012
  1. @rekado
This page is out of date. Refresh to see the latest.
View
16 src/Database/PostgreSQL/Simple/FromField.hs
@@ -54,7 +54,7 @@ import Data.Ratio (Ratio)
import Data.Time.Calendar (Day, fromGregorian)
import Data.Time.Clock (UTCTime)
import Data.Time.Format (parseTime)
-import Data.Time.LocalTime (TimeOfDay, makeTimeOfDayValid)
+import Data.Time.LocalTime (ZonedTime, TimeOfDay, makeTimeOfDayValid)
import Data.Typeable (Typeable, typeOf)
import Data.Word (Word64)
import Database.PostgreSQL.Simple.Internal
@@ -209,6 +209,20 @@ instance FromField UTCTime where
Nothing -> returnError ConversionFailed f "could not parse"
where str = preprocess (B8.unpack bs)
+instance FromField ZonedTime where
+ fromField f =
+ case oid2builtin (typeOid f) of
+ Just Timestamp -> doIt "%F %T%Q" id
+ Just TimestampWithTimeZone -> doIt "%F %T%Q%z" (++ "00")
+ _ -> const $ returnError Incompatible f "types incompatible"
+ where
+ doIt _ _ Nothing = returnError UnexpectedNull f ""
+ doIt fmt preprocess (Just bs) =
+ case parseTime defaultTimeLocale fmt str of
+ Just t -> pure t
+ Nothing -> returnError ConversionFailed f "could not parse"
+ where str = preprocess (B8.unpack bs)
+
instance FromField Day where
fromField f = atto ok date f
where ok = mkCompats [Date]
View
6 src/Database/PostgreSQL/Simple/ToField.hs
@@ -31,7 +31,7 @@ import Data.Monoid (mappend)
import Data.Time.Calendar (Day, showGregorian)
import Data.Time.Clock (UTCTime)
import Data.Time.Format (formatTime)
-import Data.Time.LocalTime (TimeOfDay)
+import Data.Time.LocalTime (TimeOfDay, ZonedTime)
import Data.Typeable (Typeable)
import Data.Word (Word, Word8, Word16, Word32, Word64)
import Database.PostgreSQL.Simple.Types (Binary(..), In(..), Null)
@@ -191,6 +191,10 @@ instance ToField UTCTime where
toField = Plain . Utf8.fromString . formatTime defaultTimeLocale "'%F %T%Q+00'"
{-# INLINE toField #-}
+instance ToField ZonedTime where
+ toField = Plain . Utf8.fromString . formatTime defaultTimeLocale "'%F %T%Q%z'"
+ {-# INLINE toField #-}
+
instance ToField Day where
toField = Plain . inQuotes . Utf8.fromString . showGregorian
{-# INLINE toField #-}
Something went wrong with that request. Please try again.