Skip to content

Commit

Permalink
Keep rational usec as rational longer to avoid rounding bugs.
Browse files Browse the repository at this point in the history
Fixes jruby#4866.
  • Loading branch information
headius committed Nov 27, 2017
1 parent 831726f commit 4be8d66
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions core/src/main/java/org/jruby/RubyTime.java
Expand Up @@ -1554,9 +1554,17 @@ private static RubyTime createTime(ThreadContext context, RubyClass klass, IRuby
boolean fractionalUSecGiven = args[6] instanceof RubyFloat || args[6] instanceof RubyRational;

if (fractionalUSecGiven) {
double micros = RubyNumeric.num2dbl(args[6]);
time.dt = dt.withMillis(dt.getMillis() + (long) (micros / 1000));
nanos = (long) Math.rint((micros * 1000) % 1000000);
if (args[6] instanceof RubyRational) {
RubyRational usecRat = (RubyRational) args[6];
RubyRational nsecRat = (RubyRational) usecRat.op_mul(context, runtime.newFixnum(1000));
double tmpNanos = nsecRat.getDoubleValue(context);
time.dt = dt.withMillis((long) (dt.getMillis() + (tmpNanos / 1000000)));
nanos = (long) tmpNanos % 1000000;
} else {
double micros = RubyNumeric.num2dbl(args[6]);
time.dt = dt.withMillis(dt.getMillis() + (long) (micros / 1000));
nanos = (long) Math.rint((micros * 1000) % 1000000);
}
} else {
int usec = int_args[4] % 1000;
int msec = int_args[4] / 1000;
Expand Down

0 comments on commit 4be8d66

Please sign in to comment.