3

I am using Rails 2.3.3 with Postgresql.

In my application environment.rb file I have set config.time_zone = 'UTC' and in my before_filter I set Time.zone = current_user.time_zone.

The problem I face is the Time that gets saved is in the users time_zone but gets saved as UTC in the database.

For example if I select 12:00 am IST (i.e +530 IST) it gets stored as 12:00 am UTC. However locally the configurations seem to work as expected and while fetching the data the time gets converted to the users time_zone so it gains 530 hours.

Would appreciate some help on this.

Thanks

Sid
  • 6,134
  • 9
  • 34
  • 57
  • So the problem is that you select a time in your form and that exact time is saved as UTC, but you expect it to be saved with the IST time offset? – Tim Brandes Jan 04 '12 at 13:26
  • No I expect the time to be converted to UTC and then saved. Here the time gets saved without getting converted to UTC time. – Sid Jan 04 '12 at 14:32
  • what do you mean by it works locally? Works on your development machine but not in production ? – Frederick Cheung Jan 04 '12 at 14:38
  • I've had a similar issue some time ago with local and production differences concerning summer and winter time. Try resolving your errors by manually calling (for example) self.time.utc in your model before saving your record and see if that helps. – Tim Brandes Jan 04 '12 at 15:45
  • Yes works on my development machine. I'm sorry if I wasn't clear. – Sid Jan 05 '12 at 04:19

1 Answers1

4

Do you use time or datetime as the database column type for your object?

This recent (and very similar) question to yours could be resolved by changing the type from time to datetime.

Community
  • 1
  • 1
Tim Brandes
  • 2,103
  • 15
  • 14
  • 2
    The solution was to use Time.zone.parse(event.start_date) which i wasn't doing. I assumed that changing the Time.zone to the user timezone would automatically take care of that which I believe is wrong. Thank you – Sid Jan 19 '12 at 07:53