Mike
Mike

Reputation: 137

RoR 4 Change timezone

How can I change the timezone to Europe/Amsterdam. I tried putting the following in application_controller.rb

config.time_zone = "Europe/Amsterdam"

Upvotes: 0

Views: 560

Answers (2)

sparkle
sparkle

Reputation: 7400

You can set custom timezone in config/application.rb

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

OR

ApplicationController

Use @country to manage which timezone you want to switch to. In this example I use "subdomains" to fetch @country. You can easily adapt to your scenario

Write this in your /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

Upvotes: 2

NARKOZ
NARKOZ

Reputation: 27911

You can set custom timezone in 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)'

Upvotes: 0

Related Questions