Blankman
Blankman

Reputation: 267140

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.

Upvotes: 10

Views: 3318

Answers (4)

apneadiving
apneadiving

Reputation: 115541

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.

Upvotes: 2

Straff
Straff

Reputation: 5759

I found

rake time:zones:all

to be really useful. It shows a list of offsets and then zone name strings under that. eg:

* UTC +12:00 * 
Auckland 
Fiji
Kamchatka
Magadan
Marshall Is.
Solomon Is.
Wellington

I needed below in application.rb (not intuitive given default time zone string of "Mountain Time (US & Canada)"):

config.time_zone = 'Wellington'

Upvotes: 0

Wizard of Ogz
Wizard of Ogz

Reputation: 12643

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.

Upvotes: 11

exine
exine

Reputation: 133

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.

Upvotes: 1

Related Questions