I'm trying to round the current time to the closest 15-min increment.
For example, if the current time is "4:37pm", I want it to round to "4:30pm". And if it were "4:41pm", it should round to "4:45pm".
The reason for this is I have an HTML select that has 15-min increments, and I want to default it to select the closest option based on time.
I've come across some code that works, but it seems clunky:
increment = 15*60 # => 900 (15 min in seconds)
current_time = Time.zone.now # => Mon, 27 Aug 2018 17:50:31 EDT -04:00
Time.at((current_time.to_r / increment).round * increment) # => 2018-08-27 17:45:00 -0400
I'm surprised there's not a built-in time function like Time.now.round_to_closest(increment_in_min: 15)
Is it there and I'm just missing it?