예제 #1
0
  @JRubyMethod(name = "at", meta = true)
  public static IRubyObject at(
      ThreadContext context, IRubyObject recv, IRubyObject arg1, IRubyObject arg2) {
    Ruby runtime = context.getRuntime();

    RubyTime time =
        new RubyTime(runtime, (RubyClass) recv, new DateTime(0L, getLocalTimeZone(runtime)));

    long seconds = RubyNumeric.num2long(arg1);
    long millisecs = 0;
    long microsecs = 0;

    long tmp = RubyNumeric.num2long(arg2);
    millisecs = tmp / 1000;
    microsecs = tmp % 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;
  }
예제 #2
0
  @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;
  }