at.
at.

Reputation: 52560

Getting the current month in a timezone in Rails on Heroku

I get the current month number using the following simple code in a Controller:

@month = DateTime.now.month

In my application_controller.rb:

around_filter :use_time_zone

def use_time_zone(&block)
  Time.use_zone('Pacific Time (US & Canada)', &block)
end

And it seems to work great. Though I did see some kind of Rails 4 deprecation warning somewhere, I think in my production logs. Maybe that's the cause of my issue?

What happens is when I deploy to Heroku, the @month I'm guessing is using UTC time instead of Pacific. It's 5:30pm March 31 right now and @month is 4 (April). But only in production on Heroku, locally (using SQLite3 if that matters), @month is correctly 3 (March).

How do I get the correct month for a particular timezone on Heroku?

Upvotes: 0

Views: 772

Answers (1)

vee
vee

Reputation: 38645

Use DateTime.now.in_time_zone('Pacific Time (US & Canada)').month or DateTime.now.in_time_zone.month. In the latter case the time zone returned by Time.zone is used.

Unlike Time.zone.now and DateTime.current that use the Rails time zone, Time.now and DateTime.now, ruby classes, use the system's time zone by default.

Upvotes: 3

Related Questions