Method: TimeMath::Units::Base#floor

Defined in:
lib/time_math/units/base.rb

#floor(tm, span = 1) ⇒ Time, ...

Rounds tm down to nearest unit (this means, TimeMath.day.floor(tm) will return beginning of tm-s day, and so on).

An optional second argument allows you to floor to arbitrary number of units, like to "each 3-hour" mark:

TimeMath.hour.floor(Time.parse('14:00'), 3)
# => 2016-06-23 12:00:00 +0300

# works well with float/rational spans
TimeMath.hour.floor(Time.parse('14:15'), 1/2r)
# => 2016-06-23 14:00:00 +0300
TimeMath.hour.floor(Time.parse('14:45'), 1/2r)
# => 2016-06-23 14:30:00 +0300

Parameters:

  • tm (Time, Date, DateTime)

    time value to floor.

  • span (Numeric) (defaults to: 1)

    how many units to floor to. For units less than week supports float/rational values.

Returns:

  • (Time, Date, DateTime)

    floored time value; class and timezone offset of origin would be preserved.



51
52
53
54
# File 'lib/time_math/units/base.rb', line 51

def floor(tm, span = 1)
  int_floor = advance(floor_1(tm), (tm.send(name) / span.to_f).floor * span - tm.send(name))
  float_fix(tm, int_floor, span % 1)
end