LoginSignup
2
3

More than 5 years have passed since last update.

15分区切りで、次の時間を知りたいとき

Last updated at Posted at 2017-09-01

こういうのをやりたい

  • 13:00 => 13:15
  • 13:05 => 13:15
  • 13:21 => 13:30
  • 23:47 => 00:00 (次の日)

Active Support を使うとヨサソウ
(日、月、年を跨ぐときもこれでおっけーなはず)

require 'date'
require 'active_support'
require 'active_support/core_ext'

QUARTER_HOUR = 15

def quarter_hour_time(date: DateTime.now)
  date.beginning_of_hour + ((date.min / QUARTER_HOUR + 1) * QUARTER_HOUR).minutes
end

now = DateTime.now
puts now
puts quarter_hour_time(date: now)

nenmatsu = DateTime.new(2017, 12, 31, 23, 55)  
puts nenmatsu                            # => 2017-12-31T23:55:00+00:00
puts quarter_hour_time(date: nenmatsu)   # => 2018-01-01T00:00:00+00:00
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3