Skip to content
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

Time#to_date returning incorrect dates for pre-Gregorian calendars #1084

Open
nightsurge opened this issue Jan 25, 2021 · 1 comment
Open

Comments

@nightsurge
Copy link

On 52-stable, Time.parse('0001-01-01').to_date Is returning Jan 3, 0001, instead of Jan 1, 0001.
In our app there are RPG/DB2 legacy tables that store 0001-01-01 as a default date. We are trying to check for that default value and it is now failing because of this bug. It appears when this string is parsed as a Date when creating the Ruby/ActiveRecord object, it must be using Time#to_date at some point, yes?

In versions prior to 50-stable, this worked correctly for us. I see that from 50-stable and newer, the Gregorian Calendar was implemented, but there should be a fallback written into the code to handle dates that exist prior to Gregorian dates 1582. Is there a config somewhere to ignore this, or an easy way for me to monkeypatch this option into 52-stable (the highest version or DB2 database connection can support).

@nightsurge
Copy link
Author

nightsurge commented Jan 25, 2021

I was able to get around this by updating the RubyJdbcConnection.java class's dateToRuby method:

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, DateTimeZone.UTC).callMethod(context, "to_datetime").callMethod(context, "to_date");
    }

Specifically, added a "to_datetime" intermediary method which fixes the calendar issue.

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

No branches or pull requests

1 participant