【问题标题】:Changing Date and Time of Heroku(ruby on rails) Server更改 Heroku(ruby on rails) 服务器的日期和时间
【发布时间】:2017-07-15 03:04:45
【问题描述】:

我正在使用托管在 Heroku 上的 ruby​​ on rails 应用程序。出于测试目的,我必须更改服务器的日期和时间。 或者手动将其设置为特定日期..? 有什么办法我可以做到这一点..? 使用控制台或任何东西。?

【问题讨论】:

    标签: ruby-on-rails heroku heroku-toolbelt


    【解决方案1】:

    我通过Timecop gem 实现了这一点,并使用around_action 来更改我的登台环境中的时间。

    module TimeTravelFilters
      extend ActiveSupport::Concern
    
      included do
        if Time::Clock.travel_ok?
          around_action :time_travel_for_request
        end
      end
    
      def time_travel_for_request
        time_travel
        yield
        time_travel_return
      end
    
      def time_travel
        if Time::Clock.fake_time
          Timecop.travel Time::Clock.fake_time
        else
          Timecop.return
        end
      end
    
      def time_travel_return
        Timecop.return
      end
    end
    

    Time::Clock 是我自己的类,用于跟踪虚假时间。

    我有一个单独的TimeController,可以让我更改服务器上的时间。

    class TimeController < ApplicationController
      before_action :require_admin!
    
      def index
      end
    
      def update
        @clock.update_attributes params[:time_clock]
        redirect_to time_index_path
      end
    
      def destroy
        @clock.reset
        redirect_to time_index_path
      end
    end
    

    【讨论】:

    • 感谢@Kevin 为我工作。它改变了 Ruby 服务器而不是 heroku 服务器的时间。每当我重新启动 Heroku 时,它都会将日期更改为当前日期。 (y)
    • 没错。它对于测试分布在一段时间内的一系列步骤很有用。
    【解决方案2】:

    您不能更改日期时间,但可以更改时区

    heroku config:add TZ="America/Los_Angeles"
    

    http://blog.pardner.com/2012/08/setting-the-default-time-zone-for-a-heroku-app/

    【讨论】:

    • 正如我所说,您不能更改日期时间,但您可以通过手动运行命令来更改时区
    【解决方案3】:

    对于在 Heroku 上运行的 Rails 应用程序,默认情况下 Time.now 和 some_time.localtime 将以 UTC 显示。如果您想为应用分配时区,可以将 TZ 配置变量设置为时区(必须为 tz 数据库时区格式)。

    heroku config:add TZ="America/Los_Angeles"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多