2

I was currently presenting an project to my professor in university and he didn't like that my rails app used DATETIME to store its timestamps in the database (he doesn't know or seem to like rails anyway :). He said this is going to lead to problems due to different timezones and I should save it as UNIX timestamps (integers) to the database.

Now I suppose the people who designed rails had a reason to store it like this, and as the timestamps fields are added automatically to any migration, I wanted to ask you guys

a) if I can modify the rails default migrations to use the UNIX timestamp by default

b) about pros and cons of this approach

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
tmaximini
  • 8,403
  • 6
  • 47
  • 69

2 Answers2

1

Rails 2.2 and later supports i18n (Internationalization) out of box. This includes handling time zones and date time formatting in the database as well as views. Have a look at the rails guide for the internationalization API here.

Specifically, you'll probably be looking into Time helpers. I'd recommend making sure that you're storing UTC format DATETIMES in the db and then using the Time helpers to translate it to the correct time for the locale of your end user.

Specify the timezone in /config/application.rb and if you use the right Time helpers, Rails will just take care of everything else for you.

Matthew Lehner
  • 3,967
  • 3
  • 26
  • 35
  • sorry for the late reply. but e.g. I have a comment with created_at field, I display it on my website (my app has a specific timezone), so it is displayed in the apps timezone, no matter from where in the world i look at the comment. So maybe I write a comment and right after it gets displayed to me wih the wrong time, how do I go about that? – tmaximini Jul 13 '11 at 10:33
  • 1
    @Frank, it sounds to me like you want to know how to detect each user's time zone. This is a different question than you posted originally, and the concept for a solution here looks pretty good http://stackoverflow.com/questions/13/how-can-i-determine-a-web-users-time-zone. You'll just need to apply it to your Rails app. – Wizard of Ogz Jul 13 '11 at 14:49
  • 1
    Another link with a Rails solution http://spongetech.wordpress.com/2009/02/27/detecting-browser-time-zone-with-rails/ – Wizard of Ogz Jul 13 '11 at 14:52
1

My opinion is contrary to your professor's. It is easy to properly handle Datetime types and timezone issues with Rails.

a) I'm not sure about changing the default, but here's a related question. Ruby on Rails: why ActiveRecord uses "datetime" type instead of "timestamp" for timestamp fields? b) Here's a related question with a good answer. In Ruby on Rails, what's the difference between DateTime, Timestamp, Time and Date?

Community
  • 1
  • 1
Wizard of Ogz
  • 12,543
  • 2
  • 41
  • 43