ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: muraken@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:91752] [Ruby trunk Feature#15653] Proposal: Add Time#floor
Date: Mon, 11 Mar 2019 06:26:41 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-77028.20190311062503.ec4e9e4e4a143000@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15653.20190311033748@ruby-lang.org

Issue #15653 has been updated by mrkn (Kenta Murata).


ActiveRecord を使うアプリケーションで、時刻をDBに入れて取り出すと秒の単位で切り捨てられます。
昔 Rails アプリケーションを作っていた頃は、この挙動のためにテストコードでの時刻の比較が面倒だったことを思い出しました。

----------------------------------------
Feature #15653: Proposal: Add Time#floor
https://bugs.ruby-lang.org/issues/15653#change-77028

* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
## 概要

`Time` の小数(ナノ秒)を指定した桁で切り捨てるメソッドの提案になります。


## 現状

* 小数を丸める `Time#round` はあるが小数を切り捨てるメソッドがない
* 精度が異なる時間を比較したり保存したい場合に小数を切り捨てたいケースがある
  * 例えば、小数の精度が3桁で扱われている DB からデータを取得したい場合など
  * Ruby 以外で時間に依存する場合に切り捨てや丸めに柔軟に対応したい


## 提案

* 指定した桁の小数を切り捨てる `Time#floor` メソッドの追加


## `Time#floor` の挙動

`Time#round` と同じような使い方になります。

```ruby
require 'time'

t = Time.utc(2010,3,30, 5,43,"25.123456789".to_r)
t.iso8601(10)           #=> "2010-03-30T05:43:25.1234567890Z"

# 引数に精度(桁数)を渡す
t.floor(0).iso8601(10)  #=> "2010-03-30T05:43:25.0000000000Z"
t.floor(1).iso8601(10)  #=> "2010-03-30T05:43:25.1000000000Z"
t.floor(2).iso8601(10)  #=> "2010-03-30T05:43:25.1200000000Z"
t.floor(3).iso8601(10)  #=> "2010-03-30T05:43:25.1230000000Z"
t.floor(4).iso8601(10)  #=> "2010-03-30T05:43:25.1234000000Z"
t.floor(5).iso8601(10)  #=> "2010-03-30T05:43:25.1234500000Z"
t.floor(6).iso8601(10)  #=> "2010-03-30T05:43:25.1234560000Z"
t.floor(7).iso8601(10)  #=> "2010-03-30T05:43:25.1234567000Z"
t.floor(8).iso8601(10)  #=> "2010-03-30T05:43:25.1234567800Z"
t.floor(9).iso8601(10)  #=> "2010-03-30T05:43:25.1234567890Z"
t.floor(10).iso8601(10) #=> "2010-03-30T05:43:25.1234567890Z"

# デフォルト引数は 0
t.floor.iso8601(10)     #=> "2010-03-30T05:43:25.0000000000Z"

t = Time.utc(1999,12,31, 23,59,59)
(t + 0.4).floor.iso8601(3)    #=> "1999-12-31T23:59:59.000Z"
(t + 0.49).floor.iso8601(3)   #=> "1999-12-31T23:59:59.000Z"
(t + 0.5).floor.iso8601(3)    #=> "1999-12-31T23:59:59.000Z"
(t + 1.4).floor.iso8601(3)    #=> "2000-01-01T00:00:00.000Z"
(t + 1.49).floor.iso8601(3)   #=> "2000-01-01T00:00:00.000Z"
(t + 1.5).floor.iso8601(3)    #=> "2000-01-01T00:00:00.000Z"

t = Time.utc(1999,12,31, 23,59,59)
(t + 0.123456789).floor(4).iso8601(6)  #=> "1999-12-31T23:59:59.123400Z"
```


## 問題点

次のケースで意図しない値が返って来ます。

```ruby
t = Time.utc(1999,12,31, 23,59,59)

# + 0.6 すると 1999-12-31T23:59:59.6000000000Z になる
# なので floor(1) を行うと 1999-12-31T23:59:59.6000000000Z になってほしいが .5 になって返ってくる
pp (t + 0.6).floor(1).iso8601(10)
# => "1999-12-31T23:59:59.5000000000Z"

# これは + 0.6 した際に桁落ちしているのが影響している
pp (t + 0.6).iso8601(10)
# => "1999-12-31T23:59:59.5999999999Z"
```

また、 `Time#round` でも似たような挙動となっています。

```ruby
t = Time.utc(1999,12,31, 23,59,59)

# + 0.5 では意図する結果になる
pp (t + 0.5).round.iso8601(10)
# => "2000-01-01T00:00:00.0000000000Z"

# + 0.6 - 0.1 で計算すると意図しない結果になる
pp (t + 0.6 - 0.1).round.iso8601(10)
# => "1999-12-31T23:59:59.0000000000Z"

# これもナノ秒が以下のような値になっている為
pp (t + 0.5).iso8601(10)
# => "1999-12-31T23:59:59.5000000000Z"
pp (t + 0.6 - 0.1).iso8601(10)
# => "1999-12-31T23:59:59.4999999999Z"
```

現状ではこれが仕様かバグか判断出来なかったのでそのまま(上記の挙動)になっています。


pull request : https://github.com/ruby/ruby/pull/2092





-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2019-03-11  6:26 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15653.20190311033748@ruby-lang.org>
2019-03-11  3:38 ` [ruby-core:91739] [Ruby trunk Feature#15653] Proposal: Add Time#floor manga.osyo
2019-03-11  4:11 ` [ruby-core:91741] " akr
2019-03-11  6:26 ` muraken [this message]
2019-03-11  6:29 ` [ruby-core:91753] " knu
2019-03-11  6:33 ` [ruby-core:91755] " matz
2019-03-11  6:39 ` [ruby-core:91757] " manga.osyo
2019-03-11 14:13 ` [ruby-core:91783] " zverok.offline
2019-03-11 14:27 ` [ruby-core:91784] " akr

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-77028.20190311062503.ec4e9e4e4a143000@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).