Add ToField Day
This commit is contained in:
@@ -7,8 +7,10 @@ module Database.PostgreSQL.Opium.ToField
|
||||
|
||||
import Data.Bits (Bits (..))
|
||||
import Data.ByteString (ByteString)
|
||||
import Data.Int (Int32)
|
||||
import Data.List (singleton)
|
||||
import Data.Text (Text)
|
||||
import Data.Time (Day, diffDays, fromGregorian)
|
||||
import Data.Word (Word32)
|
||||
import Database.PostgreSQL.LibPQ (Format (..), Oid)
|
||||
import Unsafe.Coerce (unsafeCoerce)
|
||||
@@ -48,3 +50,11 @@ instance ToField Float where
|
||||
|
||||
instance ToField Double where
|
||||
toField x = Just (Oid.doublePrecision, encodeBigEndian @Word 8 $ unsafeCoerce x, Binary)
|
||||
|
||||
toPostgresJulian :: Day -> Integer
|
||||
toPostgresJulian date = diffDays date postgresEpoch
|
||||
where
|
||||
postgresEpoch = fromGregorian 2000 1 1
|
||||
|
||||
instance ToField Day where
|
||||
toField x = Just (Oid.date, encodeBigEndian @Int32 4 $ fromIntegral $ toPostgresJulian x, Binary)
|
||||
|
||||
Reference in New Issue
Block a user