Respect the offset of OffsetTime when persisting/loading data

Description

We currently convert OffsetTime to java.sql.Time even though it is mapped as time with time zone, but as of JDBC 4.2 that type should be supported natively.

We should introduce a JdbcType for this and bind it with the correct type instead for dialects that support that DDL type. Also see

Activity

Show:

Gavin King November 12, 2022 at 12:10 AM

I have fixed the bug here. We now no longer just throw away the offset when persisting an OffsetTime.

Gavin King November 10, 2022 at 5:32 PM

I guess the first option is the one we would have to go with, since converting to UTC would mean that we would be incompatible with our own data.

Gavin King November 10, 2022 at 5:25 PM

So my suggestion would be, either:

Change line 85 of OffsetTimeJavaType to:

return (X) Time.valueOf( offsetTime.withOffsetSameInstant( OffsetDateTime.now().getOffset() ).toLocalTime() );

Or, change line 85 to:

return (X) Time.valueOf( offsetTime.withOffsetSameInstant( ZoneOffset.UTC ).toLocalTime() );

And line 143 to:

return ( (Time) value ).toLocalTime().atOffset( ZoneOffset.UTC );

WDYT?

Gavin King November 10, 2022 at 5:18 PM

So I note that the linked stack overflow question is using a timetz column in Postgres (which doesn’t really store the offset, it just converts to UTC), and a commenter replies with a link to the Postgres wiki where they tell you to “Never” use timetz.

So I don’t think we should do anything to encourage this usage.

So what’s a sensible thing to do with an OffsetTime? Currently, eyeballing the code, it looks like we just throw away the offset and send the LocalTime part to the database. This is pretty bad: it doesn’t even result in consistent interpretations of OffsetTimes in the current JVM! But this is simply a bug in OffsetTimeJavaType I would say.

Well, I guess the only things that could possibly be reasonable would be:

  1. convert it to UTC before sending it to the database, or

  2. convert it to the JVM offset before sending it to the database.

Neither are great, but UTC looks better to me.

Alternatively, we could simply deprecate support for persisting OffsetTimes, and save everyone a lot of head-scratching.

Christian Beikov November 10, 2022 at 5:14 PM

See discussion on the PR:

Fixed

Details

Assignee

Reporter

Components

Fix versions

Priority

Created November 7, 2022 at 8:55 AM
Updated December 22, 2022 at 10:59 PM
Resolved November 14, 2022 at 9:38 AM