问题
In my rails application,users can create questions and publish that, anybody from any country can response for that. We are designing database structure for that. so planning to get user timezone using some js and while answering converting that time and to store in a separate column(tz_created_at). so in created at the date will be stored in utc format, and in another column say tz_created_at the datetime will be stored as user's timezone converted time. (ie) in created_at column i have irb(main):013:0> DateTime.now.utc => Sun, 30 Mar 2014 18:54:46 +0000
And in tz_created_at column i have irb(main):015:0> DateTime.now + 6.hours(from user's timezone) => Mon, 31 Mar 2014 06:58:31 +0600
we are using sunspot solr to show some statistics in response over the time period.
so while querying for responses for a particular question i will search in tz_created_at column. is this approach is correct . please correct me if i am wrong
回答1:
Instead of doing that, you probably want to store the datetime as UTC, then store the user's timezone in another column. By doing that you can always change the timezone if you need to without affecting the true time (e.g. the user moves country). When you display a time to the user you can easily convert the time to their zone to display it.
If you're a subscriber of railscasts there's a video on it there, but if you're not a subscriber there's an older one that's free to watch that will give you a general idea of best practices.
来源:https://stackoverflow.com/questions/22748505/how-to-store-timezone-info-in-rails