Rails/Activerecord database field timezone - ruby-on-rails

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!

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!

Converting created_at UTC to Eastern Time

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.

Rails 3.2 + Ruby 1.9.3, created_at returns with wrong timezone

I upgraded to Rails 3.2 and Ruby 1.9.3 and I get a very strange behavior
I have a Model called Entry. when a new Entry is created it get inserted into the DB with the current date and time.
But when I run Entry.find_by_id(THE_ENTRY_ID).created_at I get the "correct" created at datetime but In a different TimeZone for example:
in the db:
2013-03-24 00:05:29
while in the Rails console and in the application:
Sat, 23 Mar 2013 20:05:29 EDT -04:00
Why is it suddenly returning the wrong time zone?
OK after reading the DOCS and seeing this rails casts:
http://railscasts.com/episodes/106-time-zones-revised
everything is now clear.
config.time_zone = 'Eastern Time (US & Canada)'
this setting is the actual setting that's in charged of converting all time objects to this current time zone. that is way i saw it as -4
this setting:
config.active_record.default_timezone
is the one that decides on how to save the time in the DB, but... it only gets 2 possible values: :local and :UTC and the default is :UTC
when i used the rails console to see my entry the time presented is after the conversion to 'Eastern Time' , in order to see the actual date stored in the DB use:
created_at_before_type_cast
which returned the UTC time as it is the default
and the reason i thought that in the DB its stored in my time zone +2
is because i used MySQL Workbench and its GUI just showed me the datetime values in my local machine time.
summary:
the setting that actually matters:
config.time_zone
it works just fine.
going through the DOCS is important
Check the configuration in the application.rb. If no timezone is set, rails will take the default timezone of your operating system. To fix that, set UTC as the timezone.

Rails: saving record from console with Timezone

I created a new object and saved the same through the console. I noticed that created_at and updated_at columns were in GMT format but when I do a Time.now in my console I get the time in my time zone. How can save my records from the console with my local time?
Rahul, Rails correctly goes out of its way to store dates in UTC (like GMT) format. I strongly advise against undoing this.
Rails (and ruby) provides Time.zone, Time.local and other methods that help you display the values in the correct local time. Your local time is determined by the settings of your system, but your local time isn't necessarily the same as that of other people using the system. Once you put this on a web server in the wild, people may be in different time zones. You can tell Rails which time zone to use by default in application.rb in this section
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
config.time_zone = 'Eastern Time (US & Canada)'
This might work OK for cases where most of your users are in the same timezone, but if you want users to be able to see date/times in their timezone, you'll probably want to store their zone in the User record. There's a time_zone_select helper to make it easy to capture that info.

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.

Resources