Closed felipefava closed 3 years ago
Could this change be a possible fix? 🤔
I don't know if this could possibly affect other things or create new bugs, but with this change I don't get the Java::OrgJodaTime::IllegalInstantException
error anymore.
Original code
protected IRubyObject dateToRuby(final ThreadContext context,
final Ruby runtime, final ResultSet resultSet, final int column)
throws SQLException {
final Date value = resultSet.getDate(column);
if ( value == null ) {
// FIXME: Do we really need this wasNull check here?
return resultSet.wasNull() ? context.nil : RubyString.newEmptyString(runtime);
}
if ( rawDateTime != null && rawDateTime.booleanValue() ) {
return RubyString.newString(runtime, DateTimeUtils.dateToString(value));
}
return DateTimeUtils.newDateAsTime(context, value, null).callMethod(context, "to_date");
}
New code:
protected IRubyObject dateToRuby(final ThreadContext context,
final Ruby runtime, final ResultSet resultSet, final int column)
throws SQLException {
final Date value = resultSet.getDate(column);
if ( value == null ) {
// FIXME: Do we really need this wasNull check here?
return resultSet.wasNull() ? context.nil : RubyString.newEmptyString(runtime);
}
if ( rawDateTime != null && rawDateTime.booleanValue() ) {
return RubyString.newString(runtime, DateTimeUtils.dateToString(value));
}
// This line is the only change
return DateTimeUtils.newDate(context, value);
// Or this also works
return DateTimeUtils.newDateAsTime(context, value, DateTimeZone.UTC).callMethod(context, "to_date");
}
Sorry for the delay in responding, I'm catching up now.
If we have a record with a Date column where the value of the date is the day where the Daylight Saving Time was made, we have this exception
Good find!
I don't have experience with java and I don't know if it is necessary, but why it creates a datetime and not a date?
JRuby's implementation of the Ruby Time object uses Joda Time's DateTime class.
Could this change be a possible fix?
It could be! Do you think you could turn your change into a PR (probably using the non-UTC path) and we'll work on it from there?
FWIW I know there's a few methods on DateTime for negotiating these DST boundaries, but I do not currently know the algorithm for properly handling this case.
Sure, I can create the PR in the afternoon, after work.
I think the more similar approach of the current implementation would be this
return DateTimeUtils.newDateAsTime(context, value, DateTimeZone.UTC).callMethod(context, "to_date");
In fact, I have currently a fork with that same change using it in production, and it is working fine.
What do you think? Should I go with that fix or the one it uses DateTimeUtils.newDate
?
I recently updated
activerecord 4
toactiverecord 5
in a non Rails app withjruby-9.2.7.0
. Now we have this version of the gem:I tried also upgrading the version to
52.6
but we still have the issue.The config
ActiveRecord::Base.default_timezone
value is:utc
.The first problem we faced, was that datetimes were cast wrongly, but we fixed it with this https://github.com/jruby/activerecord-jdbc-adapter#mysql-specific-notes. More details of that issue here.
However, that fix brings us the following issue.
If we have a record with a
Date
column where the value of the date is the day where the Daylight Saving Time was made, we have this exception:The important part of the above exception is this
2019-10-06T00:00:00.000 (America/Asuncion))
. To understand better the problem, in the timezoneAmerica/Asuncion
, on the2019-10-06
begun the Daylight Saving Time. And if I try to find a record with ActiveRecord that has a column date with the value2019-10-06
it raises the mentioned exception.In the code of this gem, I saw that for dates columns initialize a dateTime at the beginning fo the day:
I don't have experience with java and I don't know if it is necessary, but why it creates a datetime and not a date?
I think it shouldn't fail in this case when what I want to instantiate is a date column and not a datetime.
FYI: with
activerecord 4
andactiverecord-jdbc-adapter (= 1.3.25)
this wasn't an issuePlease let me know if I'm missing something, and thanks for the help.
More details of my problem:
The db record:
The db table: