Skip to content

Commit 9bf8c8b

Browse files
committed
Properly map java.time.Instant to TIMESTAMPTZ
Java's `Instant` type is a fixed point in time (unix epoch which is always UTC) and therefore similar to `OffsetDateTime`. Currently Instant is mapped to a `TIMESTAMP` which is incorrect. This commit changes this to `TIMESTAMPTZ` which is Postgres' type for specific points in time and is the same way the official driver handles this.
1 parent 22a898b commit 9bf8c8b

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

src/main/java/io/r2dbc/postgresql/codec/InstantCodec.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,10 @@
2525
import io.r2dbc.postgresql.util.ByteBufUtils;
2626
import reactor.util.annotation.Nullable;
2727

28-
import java.time.Instant;
29-
import java.time.LocalDate;
30-
import java.time.LocalDateTime;
31-
import java.time.ZoneId;
32-
import java.time.ZoneOffset;
28+
import java.time.*;
3329

3430
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
35-
import static io.r2dbc.postgresql.type.PostgresqlObjectId.TIMESTAMP;
31+
import static io.r2dbc.postgresql.type.PostgresqlObjectId.TIMESTAMPTZ;
3632

3733
final class InstantCodec extends AbstractTemporalCodec<Instant> {
3834

@@ -45,7 +41,7 @@ final class InstantCodec extends AbstractTemporalCodec<Instant> {
4541

4642
@Override
4743
public Parameter encodeNull() {
48-
return createNull(TIMESTAMP, FORMAT_TEXT);
44+
return createNull(TIMESTAMPTZ, FORMAT_TEXT);
4945
}
5046

5147
@Override
@@ -70,7 +66,7 @@ Instant doDecode(ByteBuf buffer, PostgresqlObjectId dataType, @Nullable Format f
7066
Parameter doEncode(Instant value) {
7167
Assert.requireNonNull(value, "value must not be null");
7268

73-
return create(TIMESTAMP, FORMAT_TEXT, () -> ByteBufUtils.encode(this.byteBufAllocator, value.toString()));
69+
return create(TIMESTAMPTZ, FORMAT_TEXT, () -> ByteBufUtils.encode(this.byteBufAllocator, value.toString()));
7470
}
7571

7672
@Override

src/test/java/io/r2dbc/postgresql/AbstractCodecIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void inetAddress() throws UnknownHostException {
180180

181181
@Test
182182
void instant() {
183-
testCodec(Instant.class, Instant.now(), "TIMESTAMP");
183+
testCodec(Instant.class, Instant.now(), "TIMESTAMPTZ");
184184
}
185185

186186
@Test

src/test/java/io/r2dbc/postgresql/codec/InstantCodecTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ void doCanDecode() {
7575
assertThat(codec.doCanDecode(TIMESTAMP, FORMAT_BINARY)).isTrue();
7676
assertThat(codec.doCanDecode(MONEY, FORMAT_TEXT)).isFalse();
7777
assertThat(codec.doCanDecode(TIMESTAMP, FORMAT_TEXT)).isTrue();
78+
assertThat(codec.doCanDecode(TIMESTAMPTZ, FORMAT_TEXT)).isTrue();
79+
assertThat(codec.doCanDecode(TIMESTAMPTZ, FORMAT_BINARY)).isTrue();
7880
}
7981

8082
@Test
@@ -95,7 +97,7 @@ void doEncode() {
9597

9698
assertThat(new InstantCodec(TEST).doEncode(instant))
9799
.hasFormat(FORMAT_TEXT)
98-
.hasType(TIMESTAMP.getObjectId())
100+
.hasType(TIMESTAMPTZ.getObjectId())
99101
.hasValue(encode(TEST, instant.toString()));
100102
}
101103

@@ -108,7 +110,7 @@ void doEncodeNoValue() {
108110
@Test
109111
void encodeNull() {
110112
assertThat(new InstantCodec(TEST).encodeNull())
111-
.isEqualTo(new Parameter(FORMAT_TEXT, TIMESTAMP.getObjectId(), NULL_VALUE));
113+
.isEqualTo(new Parameter(FORMAT_TEXT, TIMESTAMPTZ.getObjectId(), NULL_VALUE));
112114
}
113115

114116
}

0 commit comments

Comments
 (0)