Changing Date and Time of Heroku(ruby on rails) Server
Asked Answered
S

3

6

I am having ruby on rails application which is hosted on Heroku. For testing purpose I have to change Date and Time of the server. Or set it manually to particular date..? Is there any way I can do that..? Using console or anything.?

Stambul answered 24/2, 2017 at 10:13 Comment(0)
C
5

I achieved this with the Timecop gem using an around_action to change the time in my staging environment.

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 is my own class that keeps track of the fake time.

I have a separate TimeController that lets me change the time on the server.

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
Conserve answered 24/2, 2017 at 15:26 Comment(5)
Thanks @Kevin worked for me. It changed the time Ruby server not the heroku server. Whenever I restart Heroku, it changes date to current date. (y)Stambul
That's right. It's useful for testing series of steps that are spread over a period of time.Conserve
@KevinLawrence Ooh this is smart and basically exactly what I would need at the moment (testing a date-dependent workflow on Heroku) - any chance you have this as an Engine or otherwise ready to go somewhere?Fir
Sorry. I don't Note that Rail now has the time travel methods built in now so you don't need Timecop.Conserve
Ah too bad but thanks!Fir
K
2

You cant change the date time but you can change the timezone

heroku config:add TZ="America/Los_Angeles"

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

Koster answered 24/2, 2017 at 10:19 Comment(1)
as i said you cannot change date time but you can change the timezone by running command manuallyKoster
A
1

For Rails apps running on Heroku, by default Time.now and some_time.localtime will display in UTC. If you'd like to assign a timezone to your app, you can set the TZ config variable to a time zone (which must be in the tz database timezone format).

heroku config:add TZ="America/Los_Angeles"
Anear answered 24/2, 2017 at 18:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.