8000 Decoders for interval · postgres-haskell/postgres-wire@b286f3c · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit b286f3c

Browse files
Decoders for interval
1 parent 9f2ea70 commit b286f3c

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/Database/PostgreSQL/Protocol/Codecs/Decoders.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Database.PostgreSQL.Protocol.Codecs.Decoders where
33
import Data.Word
44
import Data.Int
55
import Data.Char
6-
import Data.Time (Day, UTCTime, LocalTime)
6+
import Data.Time (Day, UTCTime, LocalTime, DiffTime)
77
import qualified Data.ByteString as B
88
import qualified Data.Vector as V
99

@@ -112,8 +112,9 @@ int4 _ = getInt32BE
112112
int8 :: FieldDecoder Int64
113113
int8 _ = getInt64BE
114114

115-
-- interval :: FieldDecoder ?
116-
-- interval = undefined
115+
{-# INLINE interval #-}
116+
interval :: FieldDecoder DiffTime
117+
interval _ = intervalToDiffTime <$> getInt64BE <*> getInt32BE <*> getInt32BE
117118

118119
-- | Decodes representation of JSON as @ByteString@.
119120
{-# INLINE bsJsonText #-}

src/Database/PostgreSQL/Protocol/Codecs/Time.hs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ module Database.PostgreSQL.Protocol.Codecs.Time
55
, pgjToDay
66
, microsToUTC
77
, microsToLocalTime
8+
, intervalToDiffTime
9+
, diffTimeToInterval
810
) where
911

10-
import Data.Int (Int64)
12+
import Data.Int (Int64, Int32)
1113
import Data.Time (Day(..), UTCTime(..), LocalTime(..), DiffTime, TimeOfDay,
1214
picosecondsToDiffTime, timeToTimeOfDay,
1315
diffTimeToPicoseconds, timeOfDayToTime)
@@ -43,6 +45,17 @@ microsToLocalTime mcs =
4345
let (d, r) = mcs `divMod` microsInDay
4446
in LocalTime (pgjToDay d) (mcsToTimeOfDay r)
4547

48+
{-# INLINE intervalToDiffTime #-}
49+
intervalToDiffTime :: Int64 -> Int32 -> Int32 -> DiffTime
50+
intervalToDiffTime mcs days months = picosecondsToDiffTime . mcsToPcs $
51+
microsInDay * (fromIntegral months * daysInMonth + fromIntegral days)
52+
+ fromIntegral mcs
53+
54+
-- TODO consider adjusted encoding
55+
{-# INLINE diffTimeToInterval #-}
56+
diffTimeToInterval :: DiffTime -> (Int64, Int32, Int32)
57+
diffTimeToInterval dt = (fromIntegral $ diffTimeToMcs dt, 0, 0)
58+
4659
--
4760
-- Utils
4861
--
@@ -82,3 +95,6 @@ postgresEpoch = 2451545
8295

8396
microsInDay :: Num a => a
8497
microsInDay = 24 * 60 * 60 * 10 ^ 6
98+
99+
daysInMonth :: Num a => a
100+
daysInMonth = 30

0 commit comments

Comments
 (0)
0