【问题标题】:Rails/Postgres query rows grouped by day with time zoneRails/Postgres 查询按天和时区分组的行
【发布时间】:2013-06-20 18:18:07
【问题描述】:

我正在尝试显示过去 30 天在特定用户时区中每天的展示次数。问题是,根据时区,计数并不总是相同的,我无法在查询中反映这一点。

例如,在第一天 CDT (-5) 晚上 11:00 发生两次展示,在 CDT 上午 1:00 发生一次展示。如果您使用 UTC (+0) 进行查询,您将在第二天获得所有 3 次展示,而不是第一天两次,第二天一次。两个 CDT 时间都在 UTC 的第二天到达。

这就是我现在正在做的事情,我知道我必须在这里遗漏一些简单的东西:

start = 30.days.ago
finish = Time.now

# if the users time zone offset is less than 0 we need to make sure
# that we make it all the way to the newest data
if Time.now.in_time_zone(current_user.timezone) < 0
  start += 1.day
  finish += 1.day
end

(start.to_date...finish.to_date).map do |date|
  # get the start of the day in the user's timezone in utc so we can properly
  # query the database
  day = date.to_time.in_time_zone(current_user.timezone).beginning_of_day.utc
  [ (day.to_i * 1000), Impression.total_on(day) ]
end

印象模型:

class Impression < ActiveRecord::Base
  def self.total_on(day)
    count(conditions: [ "created_at >= ? AND created_at < ?", day, day + 24.hours ])
  end
end

我一直在查看其他帖子,似乎我可以让数据库为我处理很多繁重的工作,但我没有成功使用像 AT TIME ZONEINTERVAL 这样的东西。

我没有的东西看起来很脏,我知道我一定错过了一些明显的东西。任何帮助表示赞赏。

【问题讨论】:

  • 不清楚事物是如何存储在数据库中的。没有时区的时间戳?带时区的时间戳?时区是否在应用程序或数据库级别标准化?您甚至确定它们已正确存储吗? (例如,您是否可以在没有时区的情况下存储它们,而服务器在附加自己的时区等后存储它们)
  • 我正在使用 created_at,rails 默认设置在 api.rubyonrails.org/classes/ActiveRecord/Timestamp.html 中。它们以 UTC 格式存储。我应该补充一点,因为我的服务器的本地时区是 UTC。
  • Err... 换句话说,它们是 UTC 时区的时间戳?

标签: ruby-on-rails postgresql time zone impressions


【解决方案1】:

好的,在this awesome article 的帮助下,我想我已经弄明白了。我的问题源于不知道系统 Ruby 时间方法和时区感知 Rails 方法之间的区别。一旦我使用 around_filter like this 为用户设置了正确的时区,我就可以使用内置的 Rails 方法来大大简化代码:

# app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  around_filter :set_time_zone

  def set_time_zone
    if logged_in?
      Time.use_zone(current_user.time_zone) { yield }
    else
      yield
    end
  end
end

# app/controllers/charts_controller.rb

start = 30.days.ago
finish = Time.current

(start.to_date...finish.to_date).map do |date|
  # Rails method that uses Time.zone set in application_controller.rb
  # It's then converted to the proper time in utc
  time = date.beginning_of_day.utc
  [ (time.to_i * 1000), Impression.total_on(time) ]
end

# app/models/impression.rb

class Impression < ActiveRecord::Base
  def self.total_on(time)
    # time.tomorrow returns the time 24 hours after the instance time. so it stays UTC
    count(conditions: [ "created_at >= ? AND created_at < ?", time, time.tomorrow ])
  end
end

我可能还可以做更多事情,但我现在对此感觉好多了。

【讨论】:

    【解决方案2】:

    假设 around_filter 正确工作并在块中设置 Time.zone,您应该能够将您的查询重构为:

    class Impression < ActiveRecord::Base
      def self.days_ago(n, zone = Time.zone)
        Impression.where("created_at >= ?", n.days.ago.in_time_zone(zone))
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      相关资源
      最近更新 更多