繁体   English   中英

定义明天日期的Rails方式是什么?

[英]What is the Rails way to define tomorrow's date?

在Rails 3.2应用程序中,我有一个查询定义为返回所有事件项:due_date等于今天。

@due_today = Event.where(:due_date => Time.now.beginning_of_day..Time.now.end_of_day)

我想修改它以返回今天和明天到期的所有事件。

做这个的最好方式是什么?

我知道有几种选择:

Date.tomorrow
Date.current.tomorrow
Date.now.tomorrow
DateTime.now.tomorrow.to_date
Time.now.tomorrow.to_date
Date.current+1

我确定还有其他人。 所有这些都可以互换吗? 性能有什么不同吗? 是否存在与不同方法相关的问题? 我欢迎任何关于哪种方法最好的方法。

额外的荣誉:我还想将:due_date显示为Today HH:MM或Tomorrow HH:MM,其中HH:MM是时间。 是否有一种方法可以将Rails显示为今天或明天? 或者我需要定义自己的范围?

非常感谢!

对这些进行基准测试,我得到:

N = 100000
Benchmark.bmbm do |test|
  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") # => Coughs up an exception, Date.now doesn't exist!
  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

结果:

Rehearsal -----------------------------------------------------------------
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)
Time.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)
-------------------------------------------------------- total: 9.630000sec

                                    user     system      total        real
Date.tomorrow                   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对象,并且是一个国家英里中最快的。 它也是列表中最易读的人之一。

假设您正在使用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) # gives a day after the start time 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM