[Postgres] Accept LocalDateTime (and similar types without a timezone) as parameter for a column of type timestamptz
See original GitHub issueThis is started as an issue for Quarkus.
The problem is that when passing a LocalDateTime
(or some similar type without a timezone) as parameter of an insert query we get the following error:
io.vertx.core.impl.NoStackTraceThrowable: Parameter at position[0] with class = [java.time.LocalDateTime] and value = [2020-07-16T10:10:24.378] can not be coerced to the expected class = [java.time.OffsetDateTime] for encoding.
Timestamptz
doesn’t really store the timezone and it’s just state that everything is converted to UTC. So I’m not sure why it would be a problem to save the LocalDateTime
by assigning it an UTC timezone and convert it to an OffsetDateTime
.
This is an issue for Hibernate Reactive because we don’t know the type of the column on the database and we just assume that a timestamp will work.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:6
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Documentation: 15: 8.5. Date/Time Types - PostgreSQL
PostgreSQL assumes your local time zone for any type containing only date or time. All timezone-aware dates and times are stored internally in...
Read more >Difference between timestamps with/without time zone in ...
The differences are covered at the PostgreSQL documentation for date/time types. Yes, the treatment of TIME or TIMESTAMP differs between one WITH TIME...
Read more >What is a valid use case for using TIMESTAMP WITHOUT ...
It's valid to use timestamp and date for any timestamps and dates which are always in the same timezone, or are being stored...
Read more >Mapping between PostgreSQL and Java date/time types
util.Date , which implies knowledge of a time zone, even when they are used to represent PostgreSQL values with no time zone at...
Read more >Date and Time Handling | Npgsql Documentation
This is not the case: only a UTC timestamp is stored. There is no single PostgreSQL type that stores both a date/time and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I still don’t get what the problem is, though. As far as I understand it, there is no convertion to do and the type is always UTC.
Hibernate doesn’t know the type of the column on the database, so we don’t know when to add the timezone.
If I understand correctly, what you are asking is to add a timezone to a value so that the driver can ignore it because … checking notes … the timezone is always stored as UTC.
I’m not asking to add it to a standard because different databases might behave in different ways, but I still don’t get it why it cannot work for Postgres.
As an implementor of a fully compliant JDBC driver for PostgreSQL (http://github.com/impossibl/pgjdbc-ng)… please let me suggest allowing local times here is the wrong path.
JDBC did this in its first couple versions; assuming a lot with about timezones and how the driver should be expected to convert them. It’s impossible to get correct. One person’s “common sense” doesn’t work for everybody. This was a nightmare to implement and I really don’t want to see this mistake repeated.
The wisdom of this is seen with JDBC 4.2’s addition of
java.time
types. They explicitly disallow “local” date/times in explicit fields. E.g. you cannot insert aLocalDateTime
into atimestamp with timezone
. As far as I remember, even ZonedDateTime isn’t supported by the specification.If you want to store a
LocalDateTime
intotimestamp with timezone
convert it to your preferred timezone first and trust that the PostgreSQL will faithfully reproduce it, regardless of how it’s stored.