Skip to content

Commit 1d074de

Browse files
committed
Use LocaleDateTime codec for legacy Date-API
The current DateCodec for legacy `java.util.Date` was using `java.time.Instant` which was itself incorrectly mapping to `TIMESTAMP`. With the previous fix to `InstantCodec` the codec of `Date` had to be changed so the more correct `LocalDateTimeCodec`.
1 parent 9bf8c8b commit 1d074de

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@
2424
import io.r2dbc.postgresql.util.Assert;
2525
import reactor.util.annotation.Nullable;
2626

27-
import java.time.Instant;
27+
import java.time.LocalDateTime;
28+
import java.time.ZoneId;
2829
import java.util.Date;
2930

3031
final class DateCodec extends AbstractCodec<Date> {
3132

32-
private final InstantCodec delegate;
33+
private final LocalDateTimeCodec delegate;
3334

3435
DateCodec(ByteBufAllocator byteBufAllocator) {
3536
super(Date.class);
3637

3738
Assert.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null");
38-
this.delegate = new InstantCodec(byteBufAllocator);
39+
this.delegate = new LocalDateTimeCodec(byteBufAllocator);
3940
}
4041

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

58-
return Date.from(this.delegate.doDecode(buffer, dataType, format, Instant.class));
59+
LocalDateTime intermediary = this.delegate.doDecode(buffer, dataType, format, LocalDateTime.class);
60+
return Date.from(intermediary.atZone(ZoneId.systemDefault()).toInstant());
5961
}
6062

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

65-
return this.delegate.doEncode(value.toInstant());
67+
return this.delegate.doEncode(value.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
6668
}
6769

6870
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
import io.r2dbc.postgresql.util.ByteBufUtils;
2626
import reactor.util.annotation.Nullable;
2727

28-
import java.time.Instant;
29-
import java.time.LocalDateTime;
30-
import java.time.ZoneOffset;
28+
import java.time.*;
3129

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

5351
return decodeTemporal(buffer, dataType, format, LocalDateTime.class, temporal -> {
52+
if (temporal instanceof LocalDate) {
53+
return ((LocalDate) temporal).atStartOfDay(ZoneId.systemDefault()).toLocalDateTime();
54+
}
55+
5456
return Instant.from(temporal).atOffset(ZoneOffset.UTC).toLocalDateTime();
5557
});
5658
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ void constructorNoByteBufAllocator() {
4949

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

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

@@ -91,12 +92,13 @@ void doCanDecodeNoType() {
9192

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

9698
assertThat(new DateCodec(TEST).doEncode(date))
9799
.hasFormat(FORMAT_TEXT)
98100
.hasType(TIMESTAMP.getObjectId())
99-
.hasValue(encode(TEST, date.toInstant().toString()));
101+
.hasValue(encode(TEST, "2010-02-01T10:08:04.412"));
100102
}
101103

102104
@Test

0 commit comments

Comments
 (0)