Time.zone.now在自身调用和where语句之间的差异

时间:2012-03-23 05:30:18

标签: ruby-on-rails ruby-on-rails-3

我正在尝试在两个日期之间选择项目。 Time.zone.now似乎尊重区域值,但是当作为Active Record的一部分调用时,它不会。我没有将我的数据存储为UTC,因此指定为Time.zone.now应该返回当前指定区域的时间。如何正常工作?这种思维的逻辑是什么?

THX

ruby-1.9.2-p290 :009 > Time.zone.now
 => Thu, 22 Mar 2012 22:26:43 PDT -07:00 
ruby-1.9.2-p290 :010 > Event.where(:start_datetime => (Time.zone.now)..(Time.zone.now.end_of_day))
  Event Load (0.5ms)  SELECT `events`.* FROM `events` WHERE (`events`.`start_datetime` BETWEEN '2012-03-23 05:26:59' AND '2012-03-23 06:59:59')
 => [] 
ruby-1.9.2-p290 :011 > 

1 个答案:

答案 0 :(得分:0)

尝试以下内容:

Event.where(:start_datetime => (Time.now.in_time_zone(YOUR_TIME_ZONE))..(Time.zone.now.end_of_day.in_time_zone(YOUR_TIME_ZONE))

YOUR_TIME_ZONE like "Eastern Time (US & Canada)"