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#floor

time.floor([ndigits]) → new_time

Floors subsecond to a given precision in decimal digits (0 digits by default). It returns a new Time object. ndigits should be zero or a positive integer.

Example
t = Time.utc(2010,3,30, 5,43,25.123456789r)
t                       #=> 2010-03-30 05:43:25.123456789 UTC
t.floor                 #=> 2010-03-30 05:43:25 UTC
t.floor(0)              #=> 2010-03-30 05:43:25 UTC
t.floor(1)              #=> 2010-03-30 05:43:25.1 UTC
t.floor(2)              #=> 2010-03-30 05:43:25.12 UTC
t.floor(3)              #=> 2010-03-30 05:43:25.123 UTC
t.floor(4)              #=> 2010-03-30 05:43:25.1234 UTC

t = Time.utc(1999,12,31, 23,59,59)
(t + 0.4).floor    #=> 1999-12-31 23:59:59 UTC
(t + 0.9).floor    #=> 1999-12-31 23:59:59 UTC
(t + 1.4).floor    #=> 2000-01-01 00:00:00 UTC
(t + 1.9).floor    #=> 2000-01-01 00:00:00 UTC

t = Time.utc(1999,12,31, 23,59,59)
(t + 0.123456789).floor(4)  #=> 1999-12-31 23:59:59.1234 UTC
See also #eql? #friday? #dst? #getgm also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0