Skip to content

Commit 2c1beb4

Browse files
author
qz
committed
pgtype timetz support (assume UTC)
1 parent 5981e2c commit 2c1beb4

File tree

5 files changed

+21
-1
lines changed

5 files changed

+21
-1
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module Database.PostgreSQL.Protocol.Codecs.Decoders
1818
, numeric
1919
, bsText
2020
, time
21+
, timetz
2122
, timestamp
2223
, timestamptz
2324
, uuid
@@ -169,6 +170,13 @@ bsText = getByteString
169170
time :: FieldDecoder TimeOfDay
170171
time _ = mcsToTimeOfDay <$> getInt64BE
171172

173+
{-# INLINE timetz #-}
174+
timetz :: FieldDecoder TimeOfDay
175+
timetz _ = do
176+
t <- getInt64BE
177+
skipBytes 4
178+
return $ mcsToTimeOfDay t
179+
172180
{-# INLINE timestamp #-}
173181
timestamp :: FieldDecoder LocalTime
174182
timestamp _ = microsToLocalTime <$> getInt64BE

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Database.PostgreSQL.Protocol.Codecs.Encoders
1414
, numeric
1515
, bsText
1616
, time
17+
, timetz
1718
, timestamp
1819
, timestamptz
1920
, uuid
@@ -48,7 +49,7 @@ bytea = putByteString
4849
{-# INLINE char #-}
4950
char :: Char -> Encode
5051
char c
51-
| ord(c) >= 128 = error "Character code must be below 128"
52+
| ord c >= 128 = error "Character code must be below 128"
5253
| otherwise = (putWord8 . fromIntegral . ord) c
5354

5455
{-# INLINE date #-}
@@ -109,6 +110,10 @@ bsText = putByteString
109110
time :: TimeOfDay -> Encode
110111
time = putInt64BE . timeOfDayToMcs
111112

113+
{-# INLINE timetz #-}
114+
timetz :: TimeOfDay -> Encode
115+
timetz t = putInt64BE (timeOfDayToMcs t) <> putInt32BE 0
116+
112117
{-# INLINE timestamp #-}
113118
timestamp :: LocalTime -> Encode
114119
timestamp = putInt64BE . localTimeToMicros

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Database.PostgreSQL.Protocol.Codecs.PgTypes
1919
, numeric
2020
, text
2121
, time
22+
, timetz
2223
, timestamp
2324
, timestamptz
2425
, uuid
@@ -92,6 +93,9 @@ text = mkOids 25 1009
9293
time :: Oids
9394
time = mkOids 1083 1183
9495

96+
timetz :: Oids
97+
timetz = mkOids 1266 1270
98+
9599
timestamp :: Oids
96100
timestamp = mkOids 1114 1115
97101

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ module Database.PostgreSQL.Protocol.Codecs.Time
77
, microsToUTC
88
, microsToLocalTime
99
, mcsToTimeOfDay
10+
, mcsToDiffTime
1011
, intervalToDiffTime
1112
, diffTimeToInterval
13+
, diffTimeToMcs
1214
) where
1315

1416
import Data.Int (Int64, Int32, Int64)

tests/Codecs/QuickCheck.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ testCodecsEncodeDecode = testGroup "Codecs property 'encode . decode = id'"
111111
, mkCodecTest "numeric" PGT.numeric PE.numeric PD.numeric
112112
, mkCodecTest "text" PGT.text PE.bsText PD.bsText
113113
, mkCodecTest "time" PGT.time PE.time PD.time
114+
, mkCodecTest "timetz" PGT.timetz PE.timetz PD.timetz
114115
, mkCodecTest "timestamp" PGT.timestamp PE.timestamp PD.timestamp
115116
, mkCodecTest "timestamptz" PGT.timestamptz PE.timestamptz PD.timestamptz
116117
, mkCodecTest "uuid" PGT.uuid PE.uuid PD.uuid

0 commit comments

Comments
 (0)