Thanks to visit codestin.com
Credit goes to github.com

Skip to content

HHH-18532 Date/time related JavaType's are not always properly unwrapping into java.util.Date subclasses #8833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

cigaly
Copy link
Contributor

@cigaly cigaly commented Aug 28, 2024

See Jira issue HHH-18532

Fixing unwrapping date/time related values into java.sql.Date and java.sql.Time


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.


@hibernate-github-bot
Copy link

hibernate-github-bot bot commented Aug 28, 2024

Thanks for your pull request!

This pull request appears to follow the contribution rules.

› This message was automatically generated.

@cigaly cigaly force-pushed the HHH-18532 branch 2 times, most recently from 02fff14 to a7f1c0d Compare August 29, 2024 20:16
@cigaly cigaly marked this pull request as ready for review September 13, 2024 12:39
Copy link
Member

@gavinking gavinking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good overall but it seems to me that those calls to construct Time should be passing in milliseconds.

cigaly and others added 4 commits April 29, 2025 17:41
          - reordered matching in unwrap methods to push java.util.Date after java.sql.{Date,Time,Timestamp}
          - use new java.sql.{Date,Time}(instant.toEpochMilli()) instead of java.sql.{Date,Time}.from(instant);
            later method is not subclasses so it is always returning instance of java.util.Date
@@ -175,12 +173,8 @@ public Date wrap(Object value, WrapperOptions options) {
return null;
}

if ( value instanceof Time time ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why you removed this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request is that output Time should represent local time on January 1st 1970, but it input can be on any date, for example:

jshell> var t = new Time(System.currentTimeMillis())
t ==> 22:24:43

jshell> new Date(t.getTime())
$6 ==> Thu May 15 22:24:43 CEST 2025

This the reason why we have to treat it like any other java.util.Date subclass

jshell> var tt = T.millisToSqlTime(t.getTime())
tt ==> 22:24:43

This is now java.sql.Time instance on January 1st 1970

jshell> new Date(tt.getTime())
$11 ==> Thu Jan 01 22:24:43 CET 1970

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get that, it's just unfortunate that we would have to create copies every time we bind a java.sql.Time to a prepared statement even if the value already is correctly at the epoch date.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is true. One possibility to prevent this is to return time if it is already at epoch date, and make new copy if not. Something like

		if ( value instanceof Time time ) {
			Date date = new Date( time.getTime() );
			if ( date.getYear() == 70 && date.getMonth() == 0 && date.getDate() == 1 ) {
				return time;
			}
		}

Alternatively java.util.Calendar can be used, but I guess that this is 'cheaper' to create java.util.Date instance than java.util.Calendar

		if ( value instanceof Time time ) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime( time );
			if ( calendar.get( Calendar.YEAR ) == 1970
				 && calendar.get( Calendar.MONTH ) == Calendar.JANUARY
				 && calendar.get( Calendar.DAY_OF_MONTH ) == 1 ) {
				return time;
			}
		}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm missing something, but java.util.Date#getTime() specifies

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

So AFAIU, using the modulo trick that we have right now should work just fine. Please help me understand what I am missing. According so java docs the code should be correct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getHours() is deprecated and the Java doc also explains that it returns the TZ dependent value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct, that (and all other getXXX methods, with exception of getTime) were deprecated starting from JDK 1.1. Additional reason why java.time classes should be used.

Anyway, I see that you are using Time.valueOf(LocalTIme) (seven out of eight times in hibernate-core), so here is one example ...

jshell> var newYork = Time.valueOf(LocalTime.of(23, 0))
newYork ==> 23:00:00

jshell> newYork.getTime()
$20 ==> 100800000

jshell> Time.valueOf(LocalTime.of(12, 0))
$21 ==> 12:00:00

jshell> $21.getTime()
$22 ==> 61200000

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, I see that you are using Time.valueOf(LocalTIme) (seven out of eight times in hibernate-core), so here is one example ...

In fact, java.sql.Time#valueOf(java.time.LocalTime) will interpret the LocalTime as being a time in the time zone of the JVM, which is IMO natural given the way java.sql.Time#getTime() is specified. So to compute the milliseconds for that time since the epoch, it will have to add the time zone offset for the 1st January 1970, which is GMT-5, so it will have to add 18000000 milliseconds to get a GMT value.

I don't know where java.sql.Time#valueOf(java.time.LocalTime) is used in our code, but I agree that we have to be careful not to use that method if the LocalTime value comes from the database, because AFAIU that value would have no time zone association.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know where java.sql.Time#valueOf(java.time.LocalTime) is used in our code, but I agree that we have to be careful not to use that method if the LocalTime value comes from the database, because AFAIU that value would have no time zone association.

org.hibernate.query.hql.internal.SemanticQueryBuilder#sqlTimeLiteralFrom
org.hibernate.type.descriptor.java.JdbcTimeJavaType#wrap
org.hibernate.type.descriptor.java.JdbcTimeJavaType#fromString
org.hibernate.type.descriptor.java.JdbcTimeJavaType#fromEncodedString
org.hibernate.type.descriptor.java.LocalTimeJavaType#unwrap
org.hibernate.type.descriptor.java.OffsetTimeJavaType#unwrap
`org.hibernate.type.descriptor.jdbc.TimeUtcAsJdbcTimeJdbcType#getBinder

Anyway as Yoggi Berra used to say, it's like déjà vu all over again, so I suggest that this PR be closed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to discourage you on your quest to fix time zone related issues, but we have to be careful to fully understand real problems before doing anything.
I'm sure there is a bug hiding somewhere, but it would be best if we first start off with a real use case that fails.

Copy link
Member

@beikov beikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cigaly cigaly closed this May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants