Add FromField DiffTime and FromField TimeOfDay
This commit is contained in:
@@ -16,7 +16,13 @@ import Data.ByteString (ByteString)
|
||||
import Data.Functor (($>))
|
||||
import Data.Int (Int32)
|
||||
import Data.Proxy (Proxy (..))
|
||||
import Data.Time (Day (..))
|
||||
import Data.Time
|
||||
( Day (..)
|
||||
, DiffTime
|
||||
, TimeOfDay
|
||||
, picosecondsToDiffTime
|
||||
, timeToTimeOfDay
|
||||
)
|
||||
import Data.Text (Text)
|
||||
import Data.Word (Word32, Word64)
|
||||
import Database.PostgreSQL.LibPQ (Oid)
|
||||
@@ -125,6 +131,23 @@ instance FromField Day where
|
||||
where
|
||||
fromJulianDay x = ModifiedJulianDay $ x + 51544
|
||||
|
||||
-- | See https://www.postgresql.org/docs/current/datatype-datetime.html.
|
||||
-- Binary format: https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/adt/date.c;h=ae0f24de2c3c54eb6d0405cdb212597c2407238e;hb=HEAD#l1542.
|
||||
-- Accepts @time@.
|
||||
instance FromField DiffTime where
|
||||
validOid Proxy = Oid.time
|
||||
parseField = microsecondsToDiffTime <$> intParser
|
||||
where
|
||||
microsecondsToDiffTime :: Integer -> DiffTime
|
||||
microsecondsToDiffTime ms = picosecondsToDiffTime $ ms * 1000000
|
||||
|
||||
-- | See https://www.postgresql.org/docs/current/datatype-datetime.html.
|
||||
-- Binary format: https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/adt/date.c;h=ae0f24de2c3c54eb6d0405cdb212597c2407238e;hb=HEAD#l1542.
|
||||
-- Accepts @time@.
|
||||
instance FromField TimeOfDay where
|
||||
validOid Proxy = Oid.time
|
||||
parseField = timeToTimeOfDay <$> parseField @DiffTime
|
||||
|
||||
newtype RawField = RawField ByteString
|
||||
deriving (Eq, Show)
|
||||
|
||||
|
||||
@@ -52,3 +52,7 @@ boolean = eq $ Oid 16
|
||||
-- | Single days/dates.
|
||||
date :: Oid -> Bool
|
||||
date = eq $ Oid 1082
|
||||
|
||||
-- | Time of day.
|
||||
time :: Oid -> Bool
|
||||
time = eq $ Oid 1083
|
||||
|
||||
Reference in New Issue
Block a user