Skip to content

Commit

Permalink
fix: revert removal of toOffsetDateTime(String timestamp) fixes #Issu…
Browse files Browse the repository at this point in the history
…e 2497 (#2501)

* fix: revert removal of toOffsetDateTime(String timestamp)  fixes #Issue 2497

* resurrected OffsetDateTime toOffsetDateTime(Time t) for completeness

* move handling of UTC for string OffsetDateTime to call site to retain the original function signature of TimestampUtils.toOffsetDateTime

* check for +/- infinity before setting timezone to UTC
  • Loading branch information
davecramer committed May 2, 2022
1 parent 8444ed6 commit 4d711c2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
16 changes: 14 additions & 2 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
Expand Down Expand Up @@ -671,8 +672,19 @@ public int getConcurrency() throws SQLException {
}
} else {
// string
if (oid == Oid.TIMESTAMPTZ || oid == Oid.TIMESTAMP || oid == Oid.TIMETZ) {
return getTimestampUtils().toOffsetDateTime(castNonNull(getString(i)), oid != Oid.TIMETZ);

if (oid == Oid.TIMESTAMPTZ || oid == Oid.TIMESTAMP ) {

OffsetDateTime offsetDateTime = getTimestampUtils().toOffsetDateTime(castNonNull(getString(i)));
if ( offsetDateTime != OffsetDateTime.MAX && offsetDateTime != OffsetDateTime.MIN ) {
return offsetDateTime.withOffsetSameInstant(ZoneOffset.UTC);
} else {
return offsetDateTime;
}

}
if ( oid == Oid.TIMETZ ) {
return getTimestampUtils().toOffsetDateTime(castNonNull(getString(i)));
}
}

Expand Down
24 changes: 17 additions & 7 deletions pgjdbc/src/main/java/org/postgresql/jdbc/TimestampUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -516,17 +516,30 @@ public OffsetTime toOffsetTimeBin(byte[] bytes) throws PSQLException {
}
}

/**
* Returns the offset date time object matching the given bytes with Oid#TIMETZ.
* Not used internally anymore, function is here to retain compatibility with previous versions
*
* @param t the time value
* @return the matching offset date time
* @deprecated was used internally, and not used anymore
*/
@Deprecated
public OffsetDateTime toOffsetDateTime(Time t) {
// hardcode utc because the backend does not provide us the timezone
// hardcode UNIX epoch, JDBC requires OffsetDateTime but doesn't describe what date should be used
return t.toLocalTime().atDate(LocalDate.of(1970, 1, 1)).atOffset(ZoneOffset.UTC);
}

/**
* Parse a string and return a OffsetDateTime representing its value.
*
* @param s The ISO formated date string to parse.
* @param adaptToUTC if true the timezone is adapted to be UTC;
* this must be done for timestamp and timestamptz as they have no zone on server side
* @param s The ISO formatted date string to parse.
* @return null if s is null or a OffsetDateTime of the parsed string s.
* @throws SQLException if there is a problem parsing s.
*/
public @PolyNull OffsetDateTime toOffsetDateTime(
@PolyNull String s, boolean adaptToUTC) throws SQLException {
@PolyNull String s) throws SQLException {
if (s == null) {
return null;
}
Expand All @@ -545,9 +558,6 @@ public OffsetTime toOffsetTimeBin(byte[] bytes) throws PSQLException {
final ParsedTimestamp ts = parseBackendTimestamp(s);
OffsetDateTime result =
OffsetDateTime.of(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.nanos, ts.offset);
if (adaptToUTC) {
result = result.withOffsetSameInstant(ZoneOffset.UTC);
}
if (ts.era == GregorianCalendar.BC) {
return result.with(ChronoField.ERA, IsoEra.BCE.getValue());
} else {
Expand Down

0 comments on commit 4d711c2

Please sign in to comment.