Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails and dates, are they stored in UTC by default?

What is the best way for me to handle dates and timezones in rails?

Scenerio: I have customers who purchase products on a website from all over the world, and when they log in they will be able to choose which timezone they are from.

So I believe I should be storing everything in the database at UTC, and then on the front-end I should be converting the dates to the users set timezone preference.

Are their any gotchas with Ruby and Rails and datetimes etc?

I'm new to rails, so I am looking for guidance on how to handle this properly.

like image 541
Blankman Avatar asked Jul 09 '11 15:07

Blankman


3 Answers

Ok so take a look at your config/application.rb file.

You should find commented lines:

# 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 = 'Central Time (US & Canada)'

So default is UTC but you can set whatever ou need there.

like image 26
apneadiving Avatar answered Nov 17 '22 00:11

apneadiving


Fortunately Rails will pretty much handle things for you. As others pointed out, dates are stored by AR in UTC format. If you have a time_zone field for your users table you can do something like this:

# application.rb
config.time_zone = "Mountain Time (US & Canada)" # Default time zone

-

# application_controller.rb
before_filter :set_time_zone, :if => :logged_in?

protected
def set_time_zone
  Time.zone = current_user.time_zone if current_user.time_zone
end

All the datetimes should be shown in the proper time zone in your views.

I have had one production app that didn't like using the default time zone, but I can't remember which version of Rails/Ruby it was running.

like image 196
Wizard of Ogz Avatar answered Nov 17 '22 00:11

Wizard of Ogz


Yes, they are. In app, whenever you display date or time for user, everything you need is just adding timezone offset (for example: date + 1.hour for GMT+1). Remember that you need to take care of daylight saving, too. For efficency, consider adding 2 columns in your user table: timezone and time_offset. Then you would on each case do something like

= @order.created_at + session[:user].time_offset

Instead of always checking offset for each timezone set in profile.

like image 1
exine Avatar answered Nov 17 '22 00:11

exine