Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 - Want To Have Rails Use My Local Time Zone for created_at & updated_at

Is there actually a way to have Rails add/update rows with created_at and update_at using the current time zone as set on my server?

I have seen many Stack Overflow solutions where people stated how to display it on a Rails view with a selected time zone.

I have also found other Stack Overflow solutions stating that it should take the time zone from my server and update created_at and updated_at. This is not true at least for the Mac Mini Server I'm running on. I have it set to my local time zone. I also have config.time_zone = 'Central Time (US & Canada)' set in config/application.rb.

I want to be able to look at the raw database data in pgAdmin3 or some kind of database backup and know when records have been created in my time zone.

Any help would be appreciated. I will keep looking.

like image 852
xxx Avatar asked Mar 22 '14 17:03

xxx


3 Answers

# application.rb:
class Application < Rails::Application
  config.time_zone = 'Eastern Time (US & Canada)'
end

Time.zone      # => #<TimeZone:0x514834...>
Time.zone.name # => "Eastern Time (US & Canada)"
Time.zone.now  # => Sun, 18 May 2008 14:30:44 EDT -04:00

Please refer following link >>http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html#method-c-5B-5D

like image 121
neo-code Avatar answered Nov 16 '22 08:11

neo-code


If you want to set the db timezone and you are using ActiveRecord, then add the following in your application config:

config.active_record.default_timezone = :local

This will use your sever's timezone on the database.

like image 14
vee Avatar answered Nov 16 '22 08:11

vee


You can also run rake time:zones:all in order to list all time zones Rails knows. You can then update your setting in application.rb accordingly.

like image 2
pSkarl Avatar answered Nov 16 '22 08:11

pSkarl