@@ -5,9 +5,11 @@ module Database.PostgreSQL.Protocol.Codecs.Time
5
5
, pgjToDay
6
6
, microsToUTC
7
7
, microsToLocalTime
8
+ , intervalToDiffTime
9
+ , diffTimeToInterval
8
10
) where
9
11
10
- import Data.Int (Int64 )
12
+ import Data.Int (Int64 , Int32 )
11
13
import Data.Time (Day (.. ), UTCTime (.. ), LocalTime (.. ), DiffTime , TimeOfDay ,
12
14
picosecondsToDiffTime , timeToTimeOfDay ,
13
15
diffTimeToPicoseconds , timeOfDayToTime )
@@ -43,6 +45,17 @@ microsToLocalTime mcs =
43
45
let (d, r) = mcs `divMod` microsInDay
44
46
in LocalTime (pgjToDay d) (mcsToTimeOfDay r)
45
47
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
+
46
59
--
47
60
-- Utils
48
61
--
@@ -82,3 +95,6 @@ postgresEpoch = 2451545
82
95
83
96
microsInDay :: Num a => a
84
97
microsInDay = 24 * 60 * 60 * 10 ^ 6
98
+
99
+ daysInMonth :: Num a => a
100
+ daysInMonth = 30
0 commit comments