Exemplo n.º 1
0
  private IRubyObject opMinus(RubyTime other) {
    long time = getTimeInMillis() * 1000 + getUSec();

    time -= other.getTimeInMillis() * 1000 + other.getUSec();

    return RubyFloat.newFloat(getRuntime(), time / 1000000.0); // float number of seconds
  }
Exemplo n.º 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;
  }
Exemplo n.º 3
0
 public static IRubyObject parse_time(Ruby runtime, Time tm) {
     RubyTime time = RubyTime.newTime(runtime, tm.getTime());
     time.extend(new IRubyObject[] {runtime.getModule("TimeFormatter")});
     return (time.getUSec() != 0) ? time : runtime.getNil();
 }