1

In application.rb I have config.timezone = 'Pacific Time (US & Canada) but I'm adding a feature that needs to save objects with different timezones. Is there a way to override the default timezone? Everytime I try to save in a different timezone it defaults to Pacific.

cvdv
  • 2,691
  • 3
  • 14
  • 29
  • I'm assuming you've parsed the Time from a time string containing zone information, like so: `Time.strptime("2016-05-25 01:33 +05:30", "%Y-%m-%d %H:%M %Z")` Is your problem that ActiveRecord does not save this time object in the zone represented by %Z? – anujm May 24 '16 at 20:43
  • I'm using a jquery datetimepicker that inputs that formats it as `2016-05-25 12:00am` and ActiveRecord will save it as `Sat, 25 May 2016 00:00:00 PDT -07:00`. If I try to update that datetime attribute `Object.update_attribute(:time_field, Time.in_time_zone('Hawaii')` it will still save it in pacific time – cvdv May 24 '16 at 20:54
  • This probably happens because the ActiveRecord timezone is still set to 'Pacific Time (US & Canada)'. Refer to this answer for more information: http://stackoverflow.com/questions/6118779/how-to-change-default-timezone-for-active-record-in-rails – anujm May 24 '16 at 21:11
  • yes but is there a way to keep that default Pacific Time but also save things in a different TimeZone? – cvdv May 24 '16 at 21:21
  • I don't see any built-in provisions for that - the recommended approach is to let the database store all time information in the same time_zone and let the application perform the time_zone conversions. I guess your approach of saving the time_zone in a separate field is the way to go if you really need to do this. – anujm May 24 '16 at 21:37

2 Answers2

0

Does it default to pacific or does it calculate the time in pacific ?

Usually setting the time to a different time zone would just translate the time to what it would be in the default one. You would have to save a second variable with the time zone if you wanted to get it back to what it was and then calculate what the time would be in that time zone.

xyious
  • 1,065
  • 4
  • 13
  • 29
  • It defaults to pacific and I do currently have a timezone field to convert it over and method that uses it but it would be better if I could just save it based on the string input with a timezone – cvdv May 24 '16 at 19:25
0

If you need different time zones, you can change it with a around_filter in your controller.

Read more about it here

Pavan
  • 81
  • 3