2022/07/27追記
@koki_73 様からコメントをいただき、DateTimeクラスは非推奨になっているようです。
結論として、DateTimeは使わないようにするというのが正しい対処です。
@koki_73 様ありがとうございます!!
結論
DateTimeにおけるsince関数は現在の日付に 30日加算しているだけ です。
例えば、1月31日の1か月後を求める場合以下のような違いがあります。
方法 | 結果 |
---|---|
.since(1.month) | 3月2日 |
+ 1.month | 2月28日 |
next_month | 2月28日 |
months_since | 2月28日 |
注意
これはDateTimeの場合だけです。Dateはすべて2月28日になります。
プログラム例
irbの結果
# DateTimeクラスの場合はsince関数だけ結果が違います
a = DateTime.new(2022,1,31) # => Mon, 31 Jan 2022 00:00:00 +0000
a.since(1.months) # => Wed, 02 Mar 2022 10:29:06 +0000(!!!!!!!)
a + 1.months # => Mon, 28 Feb 2022 00:00:00 +0000
a.next_month # => Mon, 28 Feb 2022 00:00:00 +0000
a.months_since(1) # => Mon, 28 Feb 2022 00:00:00 +0000
# Dateクラスの場合はすべて2月28日になります。
a = Date.new(2022,1,31) # => Mon, 31 Jan 2022
a.since(1.months) # => Mon, 28 Feb 2022 00:00:00.000000000 JST +09:00(DateTimeクラスになる)
a + 1.months # => Mon, 28 Feb 2022
a.next_month # => Mon, 28 Feb 2022
a.months_since(1) # => Mon, 28 Feb 2022