Skip to content

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

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.

See also #to_json #to_s #to_i #to_time also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0