12
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rubyで時間を15分刻みで丸める ( 切り上げ / 切り捨て )

Posted at

rounding という gem を使えば楽。

# gem install rounding
require 'rounding'

Time.parse('2018/08/18 0:49 +09:00')
# => 2018-08-18 00:49:00 +0900

# 15分刻みで切り上げ
Time.parse('2018/08/18 0:49 +09:00').ceil_to(15.minutes)
# => 2018-08-18 01:00:00 +0900

# 15分刻みで切り捨て
Time.parse('2018/08/18 0:49 +09:00').round_to(15.minutes)
# => 2018-08-18 00:45:00 +0900

Rounding is compatible with ActiveSupport's Time extensions, but also works fine without ActiveSupport.

ActiveSupportTime とも互換性があるとのこと。

rounding は時間だけでなく、数値全般を扱える強力な gem なのでとても便利そう。

参考

brianhempel/rounding: Round by any arbitrary step in Ruby.
https://github.com/brianhempel/rounding

12
6
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
12
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?