Kathan
Kathan

Reputation: 1458

Rails 4 - in_time_zone unexpected behavior

I am using Rails 4 trying to work with the in_time_zone helper, and can't understand the weird behavior.

When converting my timestamp to UTC in localhost (or from the console), everything works as expected:

"2016-12-05 10:00 pm".to_time.in_time_zone("UTC)
=> 2016-12-06 05:00:00 UTC # this is the correct utc time

However, on my production site, it returns an incorrect time.

"2016-12-05 10:00 pm".to_time.in_time_zone("UTC)
=> 2016-12-05 22:00:00 UTC # this is incorrect 

Not sure how this is possible.. UTC should be UTC regardless. Any help understanding why this is happening would be awesome.

Upvotes: 1

Views: 221

Answers (1)

philomory
philomory

Reputation: 1767

You need to tell the software what time zone you're converting from, not just to. Since you don't, it assumes that it should convert from the system time to UTC. As it happens, it appears that the system clock on your production server is already in UTC, so it doesn't change the time at all.

Since you seem to be in UTC-7, I'm guessing you're on the west coast of the United States, so you might use code like this:

ActiveSupport::TimeZone['America/Los_Angeles'].parse("2016-12-05 10:00 pm").utc

Upvotes: 5

Related Questions