1

I have already many test cases in which we are using saved dates in UTC format. But now I have to change some reports in PDF and I did used following method

def display_user_zone_date
  model.created_at.in_time_zone(Time.now.utc_offset)
end

It is working perfectly. But issue is my test case failing. as I have hard coded date comparing with model

expect(model.display_user_zon_date).to eq 'YYYY Thu,  5 May 2021 05:00'

but it should be

expect(model.display_user_zon_date).to eq 'YYYY Thu,  5 May 2021 01:00'

I can change on local and it will work but issue is I have to run test cases on Github so again it will be different as per that machine time zones. And Is Time.zone.now gets browser time or system time. I think it is system time so how can I get browser time.

Any idea how can I fix it.

1 Answers1

2

Time.zone.now gets the system time.

I think the easiest would be to add the time zone selector via a dropdown (you could also get the browser time zone via JavaScript) see the answers here: https://stackoverflow.com/a/12326821/9595653 (beware, some of the suggested gems are outdated).

For your issue with the tests I can recommend https://github.com/travisjeffery/timecop which allows you to set a specific time for a test.

And lastly, this article has some nice overall explanations: https://thoughtbot.com/blog/its-about-time-zones

Dharman
  • 30,962
  • 25
  • 85
  • 135
Clara
  • 2,677
  • 4
  • 16
  • 31