2013-06-27 33 views
0

我拉小时数据,缓存它,然后在javascript滚动条中使用该数据。无论如何。这是主要问题。我使用的是:在rails中的时区真的让我感到困惑

config.time_zone = 'Eastern Time (US & Canada)' 

好看起来很好,当我做:

Entry.first.created_at #datetime -0500 

看起来不错,但它不是当我使用以下范围

scope :hourly, lambda {|h| 
    g = DateTime.current.beginning_of_day + h.hours 
    where(created_at: g..g+1.hours) 
    } 
    scope :hourly_by_date, lambda {|h,date| 
    g = DateTime.parse(date + " 00:00:00 Eastern Time (US & Canada)") + h.hours 
    where(created_at: g..g+1.hours) 
    } 
    scope :hours_ago, lambda {|h| 
    g = DateTime.current.beginning_of_hour - h.hours 
    where(created_at: g..g+1.hours) 
    } 

它显示的东西,说它是在午夜之前/之后创建的,当时我在下午6:06创建了该记录(-0700) 这超出了沮丧,我可以使用一些指导。

谢谢。

+0

请勿张贴生气。它只是让你看起来像一只鹅:-) – paxdiablo

+0

如果你改变'y.hours.since(x)'而不是'x + y.hours'(无论你有什么'x + y.hours'),它会起作用吗? AFAIK,'+'不会做时区,'#自''。我可能是错的... – Amadan

+0

问题不在于我的+时间不起作用,而是。但是当生成sql查询时,与使用UTC的数据库相比,它使用错误的日期/时间。 –

回答

0

你怎么样你的日期时间转换为UTC和比较值与数据库

scope :hourly, lambda {|h| 
    g = (DateTime.current.beginning_of_day + h.hours).utc 
    where(created_at: g..g+1.hours) 
    } 
    scope :hourly_by_date, lambda {|h,date| 
    g = (DateTime.parse(date + " 00:00:00 Eastern Time (US & Canada)") + h.hours).utc 
    where(created_at: g..g+1.hours) 
    } 
    scope :hours_ago, lambda {|h| 
    g = (DateTime.current.beginning_of_hour - h.hours).utc 
    where(created_at: g..g+1.hours) 
    } 
+0

由于DST,将范围设置为UTC似乎已经帮助它关闭了1个小时(Rails表示,当我在亚利桑那州时,我的服务器/站点位于MDT中,并且位于MST中)。 –