New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add FromField/ToField instance declarations for ZonedTime #19

Closed
wants to merge 1 commit into
base: master
from
Jump to file or symbol
Failed to load files and symbols.
+20 −2
Diff settings

Always

Just for now

@@ -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]
@@ -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 #-}
ProTip! Use n and p to navigate between commits in a pull request.