Skip to content

Commit

Permalink
Merge pull request #609 from sluukkonen/time-at-fixes
Browse files Browse the repository at this point in the history
Add support for Floats as the first argument in Time.at.
  • Loading branch information
headius committed Apr 16, 2013
2 parents 289d80d + e0f2f91 commit cb556ed
Showing 1 changed file with 17 additions and 26 deletions.
43 changes: 17 additions & 26 deletions src/org/jruby/RubyTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -1023,43 +1023,34 @@ public static IRubyObject at(ThreadContext context, IRubyObject recv, IRubyObjec
public static IRubyObject at(ThreadContext context, IRubyObject recv, IRubyObject arg1, IRubyObject arg2) {
Ruby runtime = context.runtime;

RubyTime time;
long seconds;

if (arg1 instanceof RubyTime && runtime.is1_9()) {
time = new RubyTime(runtime, (RubyClass) recv, ((RubyTime) arg1).getDateTime());
seconds = time.getDateTime().getMillis() / 1000;
RubyTime time = new RubyTime(runtime, (RubyClass) recv, new DateTime(0L, getLocalTimeZone(runtime)));
long millisecs;
long nanosecs = 0;

if (arg1 instanceof RubyFloat || arg1 instanceof RubyRational) {
double dbl = RubyNumeric.num2dbl(arg1);
millisecs = (long) (dbl * 1000);
nanosecs = ((long) (dbl * 1000000000)) % 1000000;
} else {
time = new RubyTime(runtime, (RubyClass) recv,
new DateTime(0L, getLocalTimeZone(runtime)));
seconds = RubyNumeric.num2long(arg1);
millisecs = RubyNumeric.num2long(arg1) * 1000;
}

long millisecs;
long nanosecs;

if (arg2 instanceof RubyFloat || arg2 instanceof RubyRational) {
double micros = RubyNumeric.num2dbl(arg2);
double nanos = micros * 1000;
millisecs = (long) (nanos / 1000000);
nanosecs = (long) (nanos % 1000000);
} else if (arg2 instanceof RubyTime && runtime.is1_9()) {
RubyTime t = (RubyTime) arg2;
// MRI treats the second argument as nanoseconds since the epoch
// However, RubyTime contains *seconds* since epoch, which is
// returned by .getMillis().
long nanos = t.getDateTime().getMillis();
millisecs = nanos / 1000000;
nanosecs = nanos % 1000000;
millisecs += (long) (nanos / 1000000);
nanosecs += (long) (nanos % 1000000);
} else {
long micros = RubyNumeric.num2long(arg2);
long nanos = micros * 1000;
millisecs = nanos / 1000000;
nanosecs = nanos % 1000000;
millisecs += nanos / 1000000;
nanosecs += nanos % 1000000;
}

time.setNSec(nanosecs);
time.dt = time.dt.withMillis(seconds * 1000 + millisecs);
long nanosecOverflow = (nanosecs / 1000000);

time.setNSec(nanosecs % 1000000);
time.dt = time.dt.withMillis(millisecs + nanosecOverflow);

time.getMetaClass().getBaseCallSite(RubyClass.CS_IDX_INITIALIZE).call(context, recv, time);

Expand Down

0 comments on commit cb556ed

Please sign in to comment.