Permalink
Browse files

add FromField/ToField instance declarations for ZonedTime

Minor tweak of 94ff9e4
See #19
  • Loading branch information...
rekado authored and lpsmith committed May 29, 2012
1 parent 9268827 commit 44de07093070eb0cc6e8da6054dfe8cabd0ef122
Showing with 19 additions and 2 deletions.
  1. +14 −1 src/Database/PostgreSQL/Simple/FromField.hs
  2. +5 −1 src/Database/PostgreSQL/Simple/ToField.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,19 @@ 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 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]
@@ -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 #-}

0 comments on commit 44de070

Please sign in to comment.