Skip to content

Commit 4d711c2

Browse files
authored
fix: revert removal of toOffsetDateTime(String timestamp) fixes #Issue 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
1 parent 8444ed6 commit 4d711c2

2 files changed

Lines changed: 31 additions & 9 deletions

File tree

pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import java.time.LocalTime;
7575
import java.time.OffsetDateTime;
7676
import java.time.OffsetTime;
77+
import java.time.ZoneOffset;
7778
import java.util.ArrayList;
7879
import java.util.Arrays;
7980
import java.util.Calendar;
@@ -671,8 +672,19 @@ public int getConcurrency() throws SQLException {
671672
}
672673
} else {
673674
// string
674-
if (oid == Oid.TIMESTAMPTZ || oid == Oid.TIMESTAMP || oid == Oid.TIMETZ) {
675-
return getTimestampUtils().toOffsetDateTime(castNonNull(getString(i)), oid != Oid.TIMETZ);
675+
676+
if (oid == Oid.TIMESTAMPTZ || oid == Oid.TIMESTAMP ) {
677+
678+
OffsetDateTime offsetDateTime = getTimestampUtils().toOffsetDateTime(castNonNull(getString(i)));
679+
if ( offsetDateTime != OffsetDateTime.MAX && offsetDateTime != OffsetDateTime.MIN ) {
680+
return offsetDateTime.withOffsetSameInstant(ZoneOffset.UTC);
681+
} else {
682+
return offsetDateTime;
683+
}
684+
685+
}
686+
if ( oid == Oid.TIMETZ ) {
687+
return getTimestampUtils().toOffsetDateTime(castNonNull(getString(i)));
676688
}
677689
}
678690

pgjdbc/src/main/java/org/postgresql/jdbc/TimestampUtils.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -516,17 +516,30 @@ public OffsetTime toOffsetTimeBin(byte[] bytes) throws PSQLException {
516516
}
517517
}
518518

519+
/**
520+
* Returns the offset date time object matching the given bytes with Oid#TIMETZ.
521+
* Not used internally anymore, function is here to retain compatibility with previous versions
522+
*
523+
* @param t the time value
524+
* @return the matching offset date time
525+
* @deprecated was used internally, and not used anymore
526+
*/
527+
@Deprecated
528+
public OffsetDateTime toOffsetDateTime(Time t) {
529+
// hardcode utc because the backend does not provide us the timezone
530+
// hardcode UNIX epoch, JDBC requires OffsetDateTime but doesn't describe what date should be used
531+
return t.toLocalTime().atDate(LocalDate.of(1970, 1, 1)).atOffset(ZoneOffset.UTC);
532+
}
533+
519534
/**
520535
* Parse a string and return a OffsetDateTime representing its value.
521536
*
522-
* @param s The ISO formated date string to parse.
523-
* @param adaptToUTC if true the timezone is adapted to be UTC;
524-
* this must be done for timestamp and timestamptz as they have no zone on server side
537+
* @param s The ISO formatted date string to parse.
525538
* @return null if s is null or a OffsetDateTime of the parsed string s.
526539
* @throws SQLException if there is a problem parsing s.
527540
*/
528541
public @PolyNull OffsetDateTime toOffsetDateTime(
529-
@PolyNull String s, boolean adaptToUTC) throws SQLException {
542+
@PolyNull String s) throws SQLException {
530543
if (s == null) {
531544
return null;
532545
}
@@ -545,9 +558,6 @@ public OffsetTime toOffsetTimeBin(byte[] bytes) throws PSQLException {
545558
final ParsedTimestamp ts = parseBackendTimestamp(s);
546559
OffsetDateTime result =
547560
OffsetDateTime.of(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.nanos, ts.offset);
548-
if (adaptToUTC) {
549-
result = result.withOffsetSameInstant(ZoneOffset.UTC);
550-
}
551561
if (ts.era == GregorianCalendar.BC) {
552562
return result.with(ChronoField.ERA, IsoEra.BCE.getValue());
553563
} else {

0 commit comments

Comments
 (0)