Skip to content

Fix instant handling #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/main/java/io/r2dbc/postgresql/codec/DateCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@
import io.r2dbc.postgresql.util.Assert;
import reactor.util.annotation.Nullable;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

final class DateCodec extends AbstractCodec<Date> {

private final InstantCodec delegate;
private final LocalDateTimeCodec delegate;

DateCodec(ByteBufAllocator byteBufAllocator) {
super(Date.class);

Assert.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null");
this.delegate = new InstantCodec(byteBufAllocator);
this.delegate = new LocalDateTimeCodec(byteBufAllocator);
}

@Override
Expand All @@ -55,14 +56,15 @@ boolean doCanDecode(PostgresqlObjectId type, Format format) {
Date doDecode(ByteBuf buffer, PostgresqlObjectId dataType, @Nullable Format format, @Nullable Class<? extends Date> type) {
Assert.requireNonNull(buffer, "byteBuf must not be null");

return Date.from(this.delegate.doDecode(buffer, dataType, format, Instant.class));
LocalDateTime intermediary = this.delegate.doDecode(buffer, dataType, format, LocalDateTime.class);
return Date.from(intermediary.atZone(ZoneId.systemDefault()).toInstant());
}

@Override
Parameter doEncode(Date value) {
Assert.requireNonNull(value, "value must not be null");

return this.delegate.doEncode(value.toInstant());
return this.delegate.doEncode(value.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
}

}
12 changes: 4 additions & 8 deletions src/main/java/io/r2dbc/postgresql/codec/InstantCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@
import io.r2dbc.postgresql.util.ByteBufUtils;
import reactor.util.annotation.Nullable;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.*;

import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
import static io.r2dbc.postgresql.type.PostgresqlObjectId.TIMESTAMP;
import static io.r2dbc.postgresql.type.PostgresqlObjectId.TIMESTAMPTZ;

final class InstantCodec extends AbstractTemporalCodec<Instant> {

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

@Override
public Parameter encodeNull() {
return createNull(TIMESTAMP, FORMAT_TEXT);
return createNull(TIMESTAMPTZ, FORMAT_TEXT);
}

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

return create(TIMESTAMP, FORMAT_TEXT, () -> ByteBufUtils.encode(this.byteBufAllocator, value.toString()));
return create(TIMESTAMPTZ, FORMAT_TEXT, () -> ByteBufUtils.encode(this.byteBufAllocator, value.toString()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
import io.r2dbc.postgresql.util.ByteBufUtils;
import reactor.util.annotation.Nullable;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.*;

import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
import static io.r2dbc.postgresql.type.PostgresqlObjectId.TIMESTAMP;
Expand All @@ -51,6 +49,10 @@ LocalDateTime doDecode(ByteBuf buffer, PostgresqlObjectId dataType, @Nullable Fo
Assert.requireNonNull(buffer, "byteBuf must not be null");

return decodeTemporal(buffer, dataType, format, LocalDateTime.class, temporal -> {
if (temporal instanceof LocalDate) {
return ((LocalDate) temporal).atStartOfDay(ZoneId.systemDefault()).toLocalDateTime();
}

return Instant.from(temporal).atOffset(ZoneOffset.UTC).toLocalDateTime();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void inetAddress() throws UnknownHostException {

@Test
void instant() {
testCodec(Instant.class, Instant.now(), "TIMESTAMP");
testCodec(Instant.class, Instant.now(), "TIMESTAMPTZ");
}

@Test
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/io/r2dbc/postgresql/codec/DateCodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ void constructorNoByteBufAllocator() {

@Test
void decode() {
Date date = Date.from(Instant.parse("2018-11-04T15:37:31.177Z"));
Instant testInstant = LocalDateTime.parse("2010-02-01T10:08:04.412").atZone(ZoneId.systemDefault()).toInstant();
Date date = Date.from(testInstant);

assertThat(new DateCodec(TEST).decode(encode(TEST, "2018-11-04 15:37:31.177"), dataType, FORMAT_TEXT, Date.class))
assertThat(new DateCodec(TEST).decode(encode(TEST, "2010-02-01 10:08:04.412"), dataType, FORMAT_TEXT, Date.class))
.isEqualTo(date);
}

Expand Down Expand Up @@ -91,12 +92,13 @@ void doCanDecodeNoType() {

@Test
void doEncode() {
Date date = new Date();
Instant testInstant = LocalDateTime.parse("2010-02-01T10:08:04.412").atZone(ZoneId.systemDefault()).toInstant();
Date date = Date.from(testInstant);

assertThat(new DateCodec(TEST).doEncode(date))
.hasFormat(FORMAT_TEXT)
.hasType(TIMESTAMP.getObjectId())
.hasValue(encode(TEST, date.toInstant().toString()));
.hasValue(encode(TEST, "2010-02-01T10:08:04.412"));
}

@Test
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/io/r2dbc/postgresql/codec/InstantCodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ void doCanDecode() {
assertThat(codec.doCanDecode(TIMESTAMP, FORMAT_BINARY)).isTrue();
assertThat(codec.doCanDecode(MONEY, FORMAT_TEXT)).isFalse();
assertThat(codec.doCanDecode(TIMESTAMP, FORMAT_TEXT)).isTrue();
assertThat(codec.doCanDecode(TIMESTAMPTZ, FORMAT_TEXT)).isTrue();
assertThat(codec.doCanDecode(TIMESTAMPTZ, FORMAT_BINARY)).isTrue();
}

@Test
Expand All @@ -95,7 +97,7 @@ void doEncode() {

assertThat(new InstantCodec(TEST).doEncode(instant))
.hasFormat(FORMAT_TEXT)
.hasType(TIMESTAMP.getObjectId())
.hasType(TIMESTAMPTZ.getObjectId())
.hasValue(encode(TEST, instant.toString()));
}

Expand All @@ -108,7 +110,7 @@ void doEncodeNoValue() {
@Test
void encodeNull() {
assertThat(new InstantCodec(TEST).encodeNull())
.isEqualTo(new Parameter(FORMAT_TEXT, TIMESTAMP.getObjectId(), NULL_VALUE));
.isEqualTo(new Parameter(FORMAT_TEXT, TIMESTAMPTZ.getObjectId(), NULL_VALUE));
}

}