Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions core/src/main/java/org/jruby/RubyTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,12 @@ public static IRubyObject at(ThreadContext context, IRubyObject recv, IRubyObjec

@JRubyMethod(meta = true)
public static IRubyObject at(ThreadContext context, IRubyObject recv, IRubyObject arg1, IRubyObject arg2) {
RubySymbol ms = context.runtime.newSymbol("microsecond");
return at(context, recv, arg1, arg2, ms);
}

@JRubyMethod(meta = true)
public static IRubyObject at(ThreadContext context, IRubyObject recv, IRubyObject arg1, IRubyObject arg2, IRubyObject arg3) {
Ruby runtime = context.runtime;

RubyTime time = new RubyTime(runtime, (RubyClass) recv, new DateTime(0L, getLocalTimeZone(runtime)));
Expand All @@ -1201,16 +1207,44 @@ public static IRubyObject at(ThreadContext context, IRubyObject recv, IRubyObjec
millisecs = RubyNumeric.num2long(arg1) * 1000;
}

if (!(arg3 instanceof RubySymbol)) {
throw context.runtime.newArgumentError("unexpected unit " + arg3);
}

RubySymbol unit = (RubySymbol) arg3;

if (arg2 instanceof RubyFloat || arg2 instanceof RubyRational) {
double micros = RubyNumeric.num2dbl(arg2);
double nanos = micros * 1000;
millisecs += (long) (nanos / 1000000);
nanosecs += (long) (nanos % 1000000);
if (runtime.newSymbol("microsecond").eql(unit) || runtime.newSymbol("usec").eql(unit)) {
double micros = RubyNumeric.num2dbl(arg2);
double nanos = micros * 1000;
millisecs += (long) (nanos / 1000000);
nanosecs += (long) (nanos % 1000000);
} else if (runtime.newSymbol("millisecond").eql(unit)) {
double millis = RubyNumeric.num2dbl(arg2);
double nanos = millis * 1000000;
millisecs += (long) (nanos / 1000000);
nanosecs += (long) (nanos % 1000000);
} else if (runtime.newSymbol("nanosecond").eql(unit) || runtime.newSymbol("nsec").eql(unit)) {
nanosecs += RubyNumeric.num2long(arg2);
} else {
throw context.runtime.newArgumentError("unexpected unit " + arg3);
}
} else {
long micros = RubyNumeric.num2long(arg2);
long nanos = micros * 1000;
millisecs += nanos / 1000000;
nanosecs += nanos % 1000000;
if (runtime.newSymbol("microsecond").eql(unit) || runtime.newSymbol("usec").eql(unit)) {
long micros = RubyNumeric.num2long(arg2);
long nanos = micros * 1000;
millisecs += nanos / 1000000;
nanosecs += nanos % 1000000;
} else if (runtime.newSymbol("millisecond").eql(unit)) {
double millis = RubyNumeric.num2long(arg2);
double nanos = millis * 1000000;
millisecs += nanos / 1000000;
nanosecs += nanos % 1000000;
} else if (runtime.newSymbol("nanosecond").eql(unit) || runtime.newSymbol("nsec").eql(unit)) {
nanosecs += RubyNumeric.num2long(arg2);
} else {
throw context.runtime.newArgumentError("unexpected unit " + arg3);
}
}

long nanosecOverflow = (nanosecs / 1000000);
Expand Down