views:

27

answers:

2

I have a database that is written to by a non-rails application and read from by a rails application. There is a datetime field in the database and the data stored in this field is stored in Eastern Time. In my rails app, I understand I can set the application's timezone in my environment.rb file by doing config.time_zone = 'Eastern Time (US & Canada)'. However, rails assumes the data stored in the database is stored in UTC and converts from config.time_zone to UTC as it pulls information in and out of the database.

Is there a way to tell rails the data in this particular field, or even all my datetime fields, is Eastern Time and not UTC?

A: 

my first suggestion is to fix the other application. The other application should be storing a UTC offset with the date. If you try working around a problem, you are just going to create more. For instance, what happens when daylight savings time goes, then are the offsets going to change?

That being said and having no other choice - I would do something along these lines, for a field created_at:

def created_at_in_eastern
  DateTime.parse(self.created_at.strftime("%D  %T EST")) if self.created_at
end

or you can use EDT depending on how the data is saved, just read the disclaimer at the top - this is a hack!

Geoff Lanotte