Skip to content

Commit

Permalink
Time#at receives 3rd argument which specifies the unit of 2nd argument
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisBr committed Mar 9, 2018
1 parent 96b264b commit 3a496c4
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions core/src/main/java/org/jruby/RubyTime.java
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

0 comments on commit 3a496c4

Please sign in to comment.