End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Time#to_r
time.to_r → a_rational
time.c
View on GitHub ↗
static VALUE
time_to_r(VALUE time)
{
struct time_object *tobj;
VALUE v;
GetTimeval(time, tobj);
v = rb_time_unmagnify_to_rational(tobj->timew);
if (!RB_TYPE_P(v, T_RATIONAL)) {
v = rb_Rational1(v);
}
return v;
}
Returns the value of time as a rational number of seconds since the Epoch.
Example
t = Time.now #=> 2020-07-20 22:03:45.212167333 +0900 t.to_r #=> (1595250225212167333/1000000000)
This method is intended to be used to get an accurate value representing the seconds (including subsecond) since the Epoch.