Stewrt Matheson
rails
django
javascript
otherstuff...
Listening to #drapht - #BrothersGrimm and feeling it. Aussie #hiphop taking steps all the time. 2 days ago
Rounding time to the closest hour in Ruby
December 9th, 2011 by

Ahhh… its one of the moments in life that make you so happy your a ruby developer.

class Time
  def round(seconds = 60)
    Time.at((self.to_f / seconds).round * seconds)
  end
 
  def floor(seconds = 60)
    Time.at((self.to_f / seconds).floor * seconds)
  end
 
  def round_to_closest_hour
    if self.min > 30
      self.round(1.hour)
    else
      self.floor(1.hour)
    end
  end
end

Source:
http://stackoverflow.com/questions/449271/how-to-round-a-time-down-to-the-nearest-15-minutes-in-ruby