Skip to content

Commit b8f208a

Browse files
committed
[refactor] move Date.new helper to utils
we should update these to faster native parts with JRuby >= 9.2
1 parent ab99560 commit b8f208a

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/java/arjdbc/mssql/MSSQLRubyJdbcConnection.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.List;
4646
import java.util.Map;
4747

48+
import arjdbc.util.DateTimeUtils;
4849
import org.joda.time.DateTime;
4950
import org.joda.time.DateTimeZone;
5051
import org.jruby.Ruby;
@@ -370,14 +371,7 @@ protected IRubyObject dateToRuby(ThreadContext context, Ruby runtime, ResultSet
370371

371372
if (value == null) return context.nil;
372373

373-
// Using Date.civil(year, month, day) so calendars get set correctly
374-
final IRubyObject[] args = {
375-
runtime.newFixnum(value.getYear() + 1900),
376-
runtime.newFixnum(value.getMonth() + 1),
377-
runtime.newFixnum(value.getDate())
378-
};
379-
380-
return runtime.getClass("Date").callMethod(context, "civil", args);
374+
return DateTimeUtils.newDate(context, value);
381375
}
382376

383377
/**

src/java/arjdbc/util/DateTimeUtils.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.joda.time.Chronology;
3232
import org.joda.time.DateTime;
3333
import org.joda.time.DateTimeZone;
34+
import org.joda.time.chrono.GJChronology;
3435
import org.joda.time.chrono.ISOChronology;
3536
import org.jruby.Ruby;
3637
import org.jruby.RubyFloat;
@@ -263,6 +264,18 @@ public static IRubyObject newDate(final ThreadContext context, final Date date,
263264
return newDate(context, year, month, day, ISOChronology.getInstance(zone));
264265
}
265266

267+
@SuppressWarnings("deprecation")
268+
public static IRubyObject newDate(final ThreadContext context, final Date date) {
269+
final int year = date.getYear() + 1900;
270+
final int month = date.getMonth() + 1;
271+
final int day = date.getDate();
272+
273+
// TODO update to using fast-path without dynamic call (after >= 9.2)
274+
final Ruby runtime = context.runtime;
275+
final IRubyObject[] args = { runtime.newFixnum(year), runtime.newFixnum(month), runtime.newFixnum(day) };
276+
return runtime.getClass("Date").callMethod(context, "civil", args);
277+
}
278+
266279
// @Deprecated
267280
public static Timestamp convertToTimestamp(final RubyFloat value) {
268281
final Timestamp timestamp = new Timestamp(value.getLongValue() * 1000); // millis
@@ -557,7 +570,7 @@ else if ( e1 == 'B' && str.charAt(start + 1) == 'C' ) {
557570
}
558571

559572
private static IRubyObject newDate(final ThreadContext context, final int year, final int month, final int day,
560-
final ISOChronology chronology) {
573+
final Chronology chronology) {
561574
// NOTE: JRuby really needs a native date.rb until than its a bit costly going from ...
562575
// java.sql.Date -> allocating a DateTime proxy, help a bit by shooting at the internals
563576
//

0 commit comments

Comments
 (0)