I Recently needed to round down a time in certain increments. This is not something I’ve ever been asked to do previously so it was an interesting problem to figure out. The resulting formula is simple:
TIME_MIN - (TIME_MIN % INCREMENT)
This will give you the previous incremental minute. ex: Given a 5 minute increment @ 3:12, the result would be 10.
time = Time.now new_min = time.min - (time.min % 5)
I happen to be solving this in a project with
ActiveSupport so there was access to
Time#change so I finished this up with: