Skip to content

Commit e3c6ea1

Browse files
committed
Fix for Rational objects in Time::at
The fix is to reassign the sec and usec arguments in the case of a Rational sec argument by splitting the Rational into numerator and denominator. This creates the same behaviour as Time::at(seconds, microseconds_with_frac).
1 parent a9d9965 commit e3c6ea1

File tree

1 file changed

+11
-1
lines changed
  • src/main/ruby/truffleruby/core

1 file changed

+11
-1
lines changed

src/main/ruby/truffleruby/core/time.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,17 @@ def at(sec, usec=undefined, unit=undefined, **kwargs)
336336
raise ArgumentError, "unexpected unit: #{unit}"
337337
end
338338

339-
usec = 0 if Primitive.undefined?(usec)
339+
if Primitive.undefined?(usec) && Primitive.object_kind_of?(sec, Rational)
340+
rational = Truffle::Type.coerce_to_exact_num(sec)
341+
seconds, fractional_seconds = rational.divmod(1)
342+
nano_fractional_seconds = fractional_seconds * 1_000_000_000
343+
344+
time = Primitive.time_at self, seconds.to_i, nano_fractional_seconds.to_i
345+
time = Primitive.time_localtime(time, offset) if offset
346+
return time
347+
elsif Primitive.undefined?(usec)
348+
usec = 0
349+
end
340350

341351
s = Truffle::Type.coerce_to_exact_num(sec)
342352
u = Truffle::Type.coerce_to_exact_num(usec)

0 commit comments

Comments
 (0)