@JRubyMethod(name = "+", required = 1, compat = CompatVersion.RUBY1_8) public IRubyObject op_plus(IRubyObject other) { if (other instanceof RubyTime) { throw getRuntime().newTypeError("time + time ?"); } long adjustment = Math.round(RubyNumeric.num2dbl(other) * 1000000); return opPlusCommon(adjustment); }
@JRubyMethod(name = "+", required = 1, compat = CompatVersion.RUBY1_9) public IRubyObject op_plus19(ThreadContext context, IRubyObject other) { checkOpCoercion(context, other); if (other instanceof RubyTime) { throw getRuntime().newTypeError("time + time ?"); } other = other.callMethod(context, "to_r"); long adjustment = new Double(RubyNumeric.num2dbl(other) * 1000000).longValue(); return opPlusCommon(adjustment); }
// c: rb_random_real public static double randomReal(ThreadContext context, IRubyObject obj) { RandomType random = null; if (obj.equals(context.runtime.getRandomClass())) { random = getDefaultRand(context); } if (obj instanceof RubyRandom) { random = ((RubyRandom) obj).random; } if (random != null) { return random.genrandReal(); } double d = RubyNumeric.num2dbl(Helpers.invoke(context, obj, "rand")); if (d < 0.0 || d >= 1.0) { throw context.runtime.newRangeError("random number too big: " + d); } return d; }
private IRubyObject opMinusCommon(IRubyObject other) { long time = getTimeInMillis(); long adjustment = Math.round(RubyNumeric.num2dbl(other) * 1000000); long micro = adjustment % 1000; adjustment = adjustment / 1000; time -= adjustment; if (getUSec() < micro) { time--; micro = 1000 - (micro - getUSec()); } else { micro = getUSec() - micro; } RubyTime newTime = new RubyTime(getRuntime(), getMetaClass()); newTime.dt = new DateTime(time).withZone(dt.getZone()); newTime.setUSec(micro); return newTime; }
@JRubyMethod(name = "at", meta = true) public static IRubyObject at(ThreadContext context, IRubyObject recv, IRubyObject arg) { Ruby runtime = context.getRuntime(); final RubyTime time; if (arg instanceof RubyTime) { RubyTime other = (RubyTime) arg; time = new RubyTime(runtime, (RubyClass) recv, other.dt); time.setUSec(other.getUSec()); } else { time = new RubyTime(runtime, (RubyClass) recv, new DateTime(0L, getLocalTimeZone(runtime))); long seconds = RubyNumeric.num2long(arg); long millisecs = 0; long microsecs = 0; // In the case of two arguments, MRI will discard the portion of // the first argument after a decimal point (i.e., "floor"). // However in the case of a single argument, any portion after // the decimal point is honored. if (arg instanceof RubyFloat || arg instanceof RubyRational) { double dbl = RubyNumeric.num2dbl(arg); long micro = Math.round((dbl - seconds) * 1000000); if (dbl < 0 && micro != 0) { micro += 1000000; } millisecs = micro / 1000; microsecs = micro % 1000; } time.setUSec(microsecs); time.dt = time.dt.withMillis(seconds * 1000 + millisecs); } time.getMetaClass().getBaseCallSites()[RubyClass.CS_IDX_INITIALIZE].call(context, recv, time); return time; }
/* Run the selector */ private int doSelect(Ruby runtime, ThreadContext context, IRubyObject timeout) { int result; cancelKeys(); try { context.getThread().beforeBlockingCall(); if (timeout.isNil()) { result = this.selector.select(); } else { double t = RubyNumeric.num2dbl(timeout); if (t == 0) { result = this.selector.selectNow(); } else if (t < 0) { throw runtime.newArgumentError("time interval must be positive"); } else { result = this.selector.select((long) (t * 1000)); } } context.getThread().afterBlockingCall(); return result; } catch (IOException ie) { throw runtime.newIOError(ie.getLocalizedMessage()); } }
public final void marshal( Invocation invocation, InvocationBuffer buffer, IRubyObject parameter) { buffer.putDouble(RubyNumeric.num2dbl(parameter)); }
public final void marshal( ThreadContext context, InvocationBuffer buffer, IRubyObject parameter) { buffer.putDouble(RubyNumeric.num2dbl(parameter)); }
public static final double doubleValue(IRubyObject parameter) { return RubyNumeric.num2dbl(parameter); }
public static final float floatValue(IRubyObject parameter) { return (float) RubyNumeric.num2dbl(parameter); }
public final int intValue(ThreadContext context, IRubyObject obj) { return Float.floatToRawIntBits((float) RubyNumeric.num2dbl(obj)); }
/** * @param ps the PreparedStatement for which the parameter should be set * @param recv * @param arg a parameter value * @param idx the index of the parameter * @throws java.sql.SQLException */ private static void setPreparedStatementParam( PreparedStatement ps, IRubyObject recv, IRubyObject arg, int idx) throws SQLException { Integer jdbcTypeId = null; try { jdbcTypeId = ps.getMetaData().getColumnType(idx); } catch (Exception ex) { } String rubyTypeName = arg.getType().getName(); if ("Fixnum".equals(rubyTypeName)) { ps.setInt(idx, Integer.parseInt(arg.toString())); } else if ("Bignum".equals(rubyTypeName)) { ps.setLong(idx, ((RubyBignum) arg).getLongValue()); } else if ("Float".equals(rubyTypeName)) { ps.setDouble(idx, RubyNumeric.num2dbl(arg)); } else if ("BigDecimal".equals(rubyTypeName)) { ps.setBigDecimal(idx, ((RubyBigDecimal) arg).getValue()); } else if ("NilClass".equals(rubyTypeName)) { // XXX In fact this should looks like ps.setNull(idx, Types.YYY); // where YYY is a JDBC type of column i.e. Types.VARCHAR // but this code works for MySQL :) ps.setNull(idx, Types.NULL); } else if ("TrueClass".equals(rubyTypeName) || "FalseClass".equals(rubyTypeName)) { ps.setBoolean(idx, arg.toString().equals("true")); } else if ("Class".equals(rubyTypeName)) { ps.setString(idx, arg.toString()); } else if ("Extlib::ByteArray".equals(rubyTypeName)) { ps.setBytes(idx, ((RubyString) arg).getBytes()); // TODO: add support for ps.setBlob(); } else if ("Date".equals(rubyTypeName)) { ps.setDate(idx, java.sql.Date.valueOf(arg.toString())); } else if ("Time".equals(rubyTypeName)) { RubyTime rubyTime = (RubyTime) arg; java.util.Date date = rubyTime.getJavaDate(); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(date); cal.setTimeZone( TimeZone.getTimeZone("UTC")); // XXX works only if driver suports Calendars in PS java.sql.Timestamp ts; if (driver.supportsCalendarsInJDBCPreparedStatement() == true) { ts = new java.sql.Timestamp(cal.getTime().getTime()); ts.setNanos(cal.get(GregorianCalendar.MILLISECOND) * 100000); } else { // XXX ugly workaround for MySQL and Hsqldb ts = new Timestamp( cal.get(GregorianCalendar.YEAR) - 1900, cal.get(GregorianCalendar.MONTH), cal.get(GregorianCalendar.DAY_OF_MONTH), cal.get(GregorianCalendar.HOUR_OF_DAY), cal.get(GregorianCalendar.MINUTE), cal.get(GregorianCalendar.SECOND), cal.get(GregorianCalendar.MILLISECOND) * 100000); } ps.setTimestamp(idx, ts, cal); } else if ("DateTime".equals(rubyTypeName)) { ps.setTimestamp( idx, java.sql.Timestamp.valueOf( arg.toString().replace('T', ' ').replaceFirst("[-+]..:..$", ""))); } else if (arg.toString().indexOf("-") != -1 && arg.toString().indexOf(":") != -1) { // TODO: improve the above string pattern checking // Handle date patterns in strings java.util.Date parsedDate; try { parsedDate = FORMAT.parse(arg.asJavaString().replace('T', ' ')); java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime()); ps.setTimestamp(idx, timestamp); } catch (ParseException ex) { ps.setString(idx, api.convertToRubyString(arg).getUnicodeValue()); } } else if (arg.toString().indexOf(":") != -1 && arg.toString().length() == 8) { // Handle time patterns in strings ps.setTime(idx, java.sql.Time.valueOf(arg.asJavaString())); } else { if (jdbcTypeId == null) { ps.setString(idx, api.convertToRubyString(arg).getUnicodeValue()); } else { // TODO: Here comes conversions like '.execute_reader("2")' // It definitly needs to be refactored... try { if (jdbcTypeId == Types.VARCHAR) { ps.setString(idx, api.convertToRubyString(arg).getUnicodeValue()); } else if (jdbcTypeId == Types.INTEGER) { ps.setObject(idx, Integer.valueOf(arg.toString()), jdbcTypeId); } else { // I'm not sure is it correct in 100% ps.setString(idx, api.convertToRubyString(arg).getUnicodeValue()); } } catch (NumberFormatException ex) { // i.e Integer.valueOf ps.setString(idx, api.convertToRubyString(arg).getUnicodeValue()); } } } }