【发布时间】:2017-07-15 03:04:45
【问题描述】:
我正在使用托管在 Heroku 上的 ruby on rails 应用程序。出于测试目的,我必须更改服务器的日期和时间。 或者手动将其设置为特定日期..? 有什么办法我可以做到这一点..? 使用控制台或任何东西。?
【问题讨论】:
标签: ruby-on-rails heroku heroku-toolbelt
我正在使用托管在 Heroku 上的 ruby on rails 应用程序。出于测试目的,我必须更改服务器的日期和时间。 或者手动将其设置为特定日期..? 有什么办法我可以做到这一点..? 使用控制台或任何东西。?
【问题讨论】:
标签: ruby-on-rails heroku heroku-toolbelt
我通过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
【讨论】:
您不能更改日期时间,但可以更改时区
heroku config:add TZ="America/Los_Angeles"
http://blog.pardner.com/2012/08/setting-the-default-time-zone-for-a-heroku-app/
【讨论】:
对于在 Heroku 上运行的 Rails 应用程序,默认情况下 Time.now 和 some_time.localtime 将以 UTC 显示。如果您想为应用分配时区,可以将 TZ 配置变量设置为时区(必须为 tz 数据库时区格式)。
heroku config:add TZ="America/Los_Angeles"
【讨论】: