明日の日付を定義するRailsの方法は何ですか?



What Is Rails Way Define Tomorrows Date



解決:

これらをベンチマークすると、次のようになります。

N = 100000 Benchmark.bmbm do |テスト| test.report( 'Date.tomorrow')do N.times do x = Date.tomorrow end end test.report( 'Date.current.tomorrow')do N.times do x = Date.current.tomorrow end end#test .report( 'Date.now.tomorrow')#=>例外が発生しました。Date.nowは存在しません! test.report( 'DateTime.now.tomorrow.to_date')do N.times do x = DateTime.now.tomorrow.to_date end end test.report( 'Time.now.tomorrow.to_date')do N.times do x = Time.now.tomorrow.to_date end end test.report( 'Date.current + 1')do N.times do x = Date.current + 1 end end test.report( 'DateTime.tomorrow')do N.times do x = DateTime.now end end end

結果:



リハーサル------------------------------------------------- ---------------- Date.tomorrow 1.640000 0.010000 1.650000(1.662668)Date.current.tomorrow 1.580000 0.000000 1.580000(1.587714)DateTime.now.tomorrow.to_date 0.360000 0.010000 0.370000(0.363281)時間.now.tomorrow.to_date 4.270000 0.010000 4.280000(4.303273)Date.current + 1 1.580000 0.010000 1.590000(1.590406)DateTime.tomorrow 0.160000 0.000000 0.160000(0.164075)------------------ --------------------------------------合計:9.630000秒ユーザーシステム合計実際の日付。明日1.590000 0.000000 1.590000(1.601091)Date.current.tomorrow 1.610000 0.010000 1.620000(1.622415)DateTime.now.tomorrow.to_date 0.310000 0.000000 0.310000(0.319628)Time.now.tomorrow.to_date 4.120000 0.010000 4.130000(4.145556)Date.current + 1 1.590000 0.000000 1.590000 (1.596724)DateTime.tomorrow 0.140000 0.000000 0.140000(0.137487)

提案のリストから、DateTime.now.tomorrow.to_dateの方が高速です。

私が追加した最後のオプションをチェックしてください。それはDateオブジェクトを返し、カントリーマイルで最も速いものです。また、リストの中で最も人間が読める形式の1つでもあります。



MYSQLを使用していると仮定すると、MySQLのBETWEEN()関数を使用すると、クエリが高速になる可能性があります。

@due_today = Event.where( 'due_date BETWEEN?AND?'、DateTime.today、DateTime.tomorrow)

events.due_dateにインデックスがあるかどうか、またはBETWEENが引き続きこれらを使用するかどうかはわかりませんが。大きなデータセットでどちらが速いかを確認するには、両方のベンチマークを行う必要があります。

それがお役に立てば幸いですか?




これはどう?

1.day.from_now 2.days.from_now 3.days.from_now

与えられた時間をインクリメントしたい場合..

1.day.from_now(start_time)#開始時刻の翌日を指定します