OPENJPA-2854 fix OffsetTime handling for PostgreSQL

PostgreSQL doesn't natively support OffsetTime. While it has a column type
time with time zone it actually only stores the time as UTC time.
This commit is contained in:
Mark Struberg 2021-04-01 17:02:25 +02:00
parent 20faa3692c
commit c3bbb92557
1 changed files with 26 additions and 0 deletions

View File

@ -38,6 +38,8 @@ import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
@ -750,6 +752,30 @@ public class PostgresDictionary extends DBDictionary {
return rs.getObject(column, OffsetDateTime.class);
}
/**
* default column type for OffsetTime is 'time with time zone'.
* But opposed to the name PostgreSQL internally stores those values in UTC time
* without any timezone.
*/
@Override
public void setOffsetTime(PreparedStatement stmnt, int idx, OffsetTime val, Column col) throws SQLException {
// this is really a whacky hack somehow
// PostgreSQL doesn't support OffsetTime natively.
// The JDBC driver will automatically convert this to UTC which is the
// internal normalised TimeZone PostgreSQL uses.
LocalTime utcTime = val.withOffsetSameInstant(OffsetDateTime.now().getOffset()).toLocalTime();
stmnt.setTime(idx, java.sql.Time.valueOf(utcTime));
}
@Override
public OffsetTime getOffsetTime(ResultSet rs, int column) throws SQLException {
final java.sql.Time utcTime = rs.getTime(column);
if (utcTime != null) {
return utcTime.toLocalTime().atOffset(OffsetDateTime.now().getOffset());
}
return null;
}
@Override
public void setLocalDate(PreparedStatement stmnt, int idx, LocalDate val, Column col) throws SQLException {
stmnt.setObject(idx, val);