Converting created_at UTC to Eastern Time - ruby-on-rails

My model has (by default) generated a "created_at" field in the database that stores when the row was inserted into the database in UTC.
In my controller, I retrieve the data from the database and try to convert the value to Eastern time with the following:
data["created_at"] = data["created_at"].in_time_zone("Eastern Time (US & Canada)")
When I view the output of the above code, it looks like my "created_at" value is still in UTC instead of Eastern time. Any thoughts? Thanks.
EDIT:
To answer a comment, when I inspect the value of created_at coming from the database, it's value is: 2015-01-31 18:34:51

In your config in application.rb add timezone
config.time_zone = 'Eastern Time (US & Canada)'
it will keep your dates in db in utc, but display with specified timezone

The timestamp is still the timestamp. You're simply specifying a possible way to display it.
What might make more sense is to leave it in UTC in the database (a good standard) and just localize it upon display in whatever way makes sense.
For example, with a logging app we wrote, hovering over the UTC timestamp gives you the localized timestamp for yourself based on your user profile in the system.

Related

created_at and updated_at saving as tomorrow

When objects are created in our Rails app, it's saving in the Postgres DB 7 hours ahead of our time (PDT). Looks like it's saving everything to UTC. This really screws up analytic graphs, ect.
I have these set in application.rb
config.time_zone = 'Pacific Time (US & Canada)'
config.active_record.default_timezone = :local
How can I get everything to save/timestamp to the PDT timezone?
It is (probably) being saved at the timezone of your server, and is independent of your code. IE, it's your database setting the created_at/updated_at date time (which is defaulting to UTC)
What you need to do is make your analytics work with UTC and translate to PDT, not vice versa. That gives you a solution that is totally independent of where you are hosted, daylight savings time changes, where your users are, etc.
Consider the simple case of if you have users in multiple time zones. Someone in EST will hate you west coast PDT people 'cause they always have to translate it ;)
All in all, use and work with UTC - it's... Universal!

Rails application stores incorrect time in datetime field

Whenever I create a model, the two datetime fields are created (created_at and updated_at). Whenever I create a new instance of the object the times for those two fields are 5 hours ahead of my current time. I set config.time_zone = 'Central Time (US & Canada)' inside config/locales/application.rb but that doesn't help.
thanks,
mike
By design, active record always stores UTC dates in the database. These are converted into the timezone specified by Time.zone (which defaults to config.time_zone) when displayed to the user. Similarly if you had a date/time select in one of your forms then that time would be converted from Time.zone to UTC before storage in the database.
Assuming that DST is in effect where you're based then 5 hours ahead of your local time is UTC.

Rails ActiveRecord not converting time to UTC but saving unconverted time as UTC

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
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.

Rails 3.1 time zone confused?

Here is my current situation:
I have a user class that has an attribute time zone.
When the user creates a Lecture with start_time (3pm) and end_time (5pm), I want to ensure that in the database the start_time and end_time are actually 3pm and 5pm, in the user's time zone.
In my application controller I'm doing the following:
def set_timezone
if current_user
Time.zone = current_user.time_zone or "Eastern Time (US & Canada)"
end
end
When the above lecture is saved to the database it seems to add 4 hours. Is it converting it back to UTC? The odd thing is that when I display the time in the view it is correct (I'm guessing it's converted from the UTC time back to the EST time).
I want it so that that if a user selects 4pm it is saved in the database as 4pm AND when I display that time in the view it is 4pm. What am I currently doing wrong?
EDIT: it appears that rails also converts all times to UTC when storing them in the database. Which is ok until I do a query that involves time (I'll have to manually convert it to UTC).
MySQL also has a time zone.
Notably (emphasis mine):
The current session time zone setting affects display and storage of time values that are zone-sensitive. This includes the values displayed by functions such as NOW() or CURTIME(), and values stored in and retrieved from TIMESTAMP columns. Values for TIMESTAMP columns are converted from the current time zone to UTC for storage, and from UTC to the current time zone for retrieval.
Whether or not it's worth doing anything about it... not sure. I'm skeptical.

Rails/Activerecord database field timezone

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?
On Rails 3.0.5 in application.rb, this works:
config.time_zone = 'Central Time (US & Canada)'
config.active_record.default_timezone = :local
config.active_record.default_timezone - Tells ActiveRecord to interpret database times as being in the specified time zone, and to use this timezone for working with the created_at & updated_at attributes.
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!

Resources