RoR 4更改时区

时间:2013-12-09 11:15:22

标签: ruby-on-rails-4

如何将时区更改为欧洲/阿姆斯特丹。 我尝试将以下内容放在application_controller.rb

config.time_zone = "Europe/Amsterdam"

2 个答案:

答案 0 :(得分:2)

您可以在config/application.rb

中设置自定义时区
config.time_zone = 'Central Time (US & Canada)'

<强>的ApplicationController

使用 @country 管理您要切换到的时区。 在此示例中,我使用“子域”来获取 @country 。您可以轻松适应您的场景

将此内容写入/app/controller/application_controller.rb

    class ApplicationController < ActionController::Base

      before_filter :set_country
      before_filter :set_time_zone

    private

      def set_country
         if request.subdomains.empty? || request.subdomains.first == 'www'
          redirect_to(:subdomain => "uk")
         else
           @country ||= request.subdomains.first.upcase
         end
         @country ||= "IT"
       end

      def set_time_zone
        case @country
        when 'IT'
          Time.zone = 'Rome'
        when 'US'
          Time.zone = 'Central Time (US & Canada)'
        when 'UK'
          Time.zone = 'London'
        end
      end
  end

答案 1 :(得分:0)

您可以在config/application.rb

中设置自定义时区
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'