1

在 Rails 中,我首先有以下用于 activerecord 的配置。

config.active_record.default_timezone = :utc

现在,我想使用本地时区,所以我将其更改为:

config.active_record.default_timezone = :local

问题是,我需要将日期/日期时间列中的所有现有数据转移到本地时区。实现这一目标的最佳方法是什么?


为什么我必须这样做是因为我必须在本地时区上进行聚合,例如 :group => 'DATE(created_at)', GROUP BY DATE(created_at) 将基于 UTC,但我想聚合当地时区一天。

我知道如何编写迁移文件来迁移某个日期时间列。但是这样的专栏有很多,所以我正在寻找更好的解决方案。

4

4 回答 4

3

这很危险,但这是我在迁移中要做的:

class MigrateDateTimesFromUTCToLocal < ActiveRecord::Migration

  def self.up
    # Eager load the application, in order to find all the models
    # Check your application.rb's load_paths is minimal and doesn't do anything adverse
    Rails.application.eager_load!

    # Now all the models are loaded. Let's loop through them
    # But first, Rails can have multiple models inheriting the same table
    # Let's get the unique tables
    uniq_models = ActiveRecord::Base.models.uniq_by{ |model| model.table_name }

    begin
      # Now let's loop
      uniq_models.each do |model|
        # Since we may be in the middle of many migrations,
        # Let's refresh the latest schema for that model
        model.reset_column_information

        # Filter only the date/time columns
        datetime_columns = model.columns.select{ |column| [ :datetime, :date, :time].include? column.type }

        # Process them
        # Remember *not* to loop through model.all.each, or something like that
        # Use plain SQL, since the migrations for many columns in that model may not have run yet
        datetime_columns.each do |column|
          execute <<-SQL
            UPDATE #{model.table_name} SET #{column.name} = /* DB-specific date/time conversion */
          SQL
        end

      rescue
        # Probably time to think about your rescue strategy
        # If you have tested the code properly in Test/Staging environments
        # Then it should run fine in Production
        # So if an exception happens, better re-throw it and handle it manually
      end

    end
  end
end
于 2013-05-05T16:30:34.497 回答
1

我的第一个建议是强烈建议您不要这样做。你正在向一个受伤的世界敞开大门。也就是说,这就是你想要的:

class ShootMyFutureSelfInTheFootMigration
  def up
    Walrus.find_each do |walrus|
      married_at_utc = walrus.married_at
      walrus.update_column(:married_at, married_at_utc.in_time_zone)
    end
  end

  def down
    Walrus.find_each do |walrus|
      married_at_local = walrus.married_at
      walrus.update_column(:married_at, married_at_local.utc)
    end
  end
end

您可以将首选时区传入 DateTime#in_time_zone,如下所示:

central_time_zone = ActiveSupport::TimeZone.new("Central Time (US & Canada)")
walrus.update_column(:married_at, married_at_utc.in_time_zone(central_time_zone))

或者你可以离开它,Rails 将使用你当前的时区。请注意,这不是您所在的位置,而是您的服务器所在的位置。因此,如果您在冰岛和上海有用户,但您的服务器在加利福尼亚,则每个“本地”时区都是美国太平洋时间。

于 2013-04-30T17:53:34.910 回答
0

必须更改数据库中的数据吗?您可以改为显示本地时区的日期吗?这有帮助吗:在 Rails 3 中将 UTC 转换为本地时间

于 2013-02-28T02:14:52.590 回答
0

就像许多其他人所说的那样,您可能不想这样做。

您可以在分组之前将时间转换为不同的区域,所有这些都在数据库中。例如,使用 postgres,转换为山区标准时间:

SELECT COUNT(id), DATE(created_at AT TIME ZONE 'MST') AS created_at_in_mst
FROM users GROUP BY created_at_in_mst;
于 2013-05-04T17:29:25.190 回答