End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
Time#to_f
static VALUE
time_to_f(VALUE time)
{
struct time_object *tobj;
GetTimeval(time, tobj);
return rb_Float(rb_time_unmagnify_to_float(tobj->timew));
}
Returns the value of time as a floating point number of seconds since the Epoch. The return value approximate the exact value in the Time object because floating point numbers cannot represent all rational numbers exactly.
t = Time.now #=> 2020-07-20 22:00:29.38740268 +0900 t.to_f #=> 1595250029.3874028 t.to_i #=> 1595250029
Note that IEEE 754 double is not accurate enough to represent the exact number of nanoseconds since the Epoch. (IEEE 754 double has 53bit mantissa. So it can represent exact number of nanoseconds only in 2 ** 53 / 1_000_000_000 / 60 / 60 / 24 = 104.2 days.) When Ruby uses a nanosecond-resolution clock function, such as clock_gettime of POSIX, to obtain the current time, Time#to_f can lose information of a Time object created with Time.now.