Skip to content

Test case for issue 578 #579

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/main/java/io/r2dbc/postgresql/codec/InstantCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class InstantCodec extends AbstractTemporalCodec<Instant> {
private final Supplier<ZoneId> zoneIdSupplier;

InstantCodec(ByteBufAllocator byteBufAllocator, Supplier<ZoneId> zoneIdSupplier) {
super(Instant.class, byteBufAllocator, TIMESTAMPTZ, TIMESTAMPTZ_ARRAY, Instant::toString);
super(Instant.class, byteBufAllocator, TIMESTAMPTZ, TIMESTAMPTZ_ARRAY, PostgresqlDateTimeFormatter::format);
this.zoneIdSupplier = zoneIdSupplier;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,30 @@

package io.r2dbc.postgresql.codec;

import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.format.SignStyle;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalQuery;

import static java.time.ZoneOffset.UTC;
import static java.time.format.TextStyle.SHORT;
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
import static java.time.temporal.ChronoField.ERA;
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
import static java.time.temporal.ChronoField.YEAR;
import static java.time.temporal.ChronoField.YEAR_OF_ERA;

class PostgresqlDateTimeFormatter {

private static final DateTimeFormatter FULL_OFFSET =
new DateTimeFormatterBuilder()
.appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
.appendValue(YEAR_OF_ERA, 4, 10, SignStyle.NEVER)
.appendLiteral('-')
.appendValue(MONTH_OF_YEAR, 2)
.appendLiteral('-')
Expand All @@ -52,11 +57,15 @@ class PostgresqlDateTimeFormatter {
.optionalStart()
.appendOffset("+HH:MM:ss", "+00:00:00")
.optionalEnd()
.optionalStart()
.appendLiteral(' ')
.appendText(ERA, SHORT)
.optionalEnd()
.toFormatter();

private static final DateTimeFormatter SHORT_OFFSET =
new DateTimeFormatterBuilder()
.appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
.appendValue(YEAR_OF_ERA, 4, 10, SignStyle.NEVER)
.appendLiteral('-')
.appendValue(MONTH_OF_YEAR, 2)
.appendLiteral('-')
Expand All @@ -74,6 +83,10 @@ class PostgresqlDateTimeFormatter {
.optionalStart()
.appendOffset("+HH:mm", "+00:00")
.optionalEnd()
.optionalStart()
.appendLiteral(' ')
.appendText(ERA, SHORT)
.optionalEnd()
.toFormatter();

/**
Expand Down Expand Up @@ -104,4 +117,11 @@ static <T> T parse(CharSequence text, TemporalQuery<T> query) {
}
}

static String format(Temporal temporal) {
if (temporal instanceof Instant) {
temporal = ((Instant) temporal).atOffset(UTC);
}
return SHORT_OFFSET.format(temporal);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ void inetAddressArray() throws UnknownHostException {
@Test
void instant() {
testCodec(Instant.class, Instant.now().truncatedTo(ChronoUnit.MICROS), "TIMESTAMPTZ");
testCodec(Instant.class, Instant.parse("0000-12-31T01:01:00Z"), "TIMESTAMPTZ");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void doEncode() {
assertThat(this.codec.doEncode(instant))
.hasFormat(FORMAT_TEXT)
.hasType(TIMESTAMPTZ.getObjectId())
.hasValue(encode(TEST, instant.toString()));
.hasValue(encode(TEST, PostgresqlDateTimeFormatter.format(instant)));
}

@Test
Expand Down