mirror of https://github.com/apache/openjpa.git
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:
parent
20faa3692c
commit
c3bbb92557
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue