I am using Ruby 1.8.7 and Rails 3.0.3. Even though currently all of my users are in the same time zone as my server, I thought that in preparation for world domination I'd get rid of all of my RubyTimeObjIGotOutOfMyDb.getlocal
calls and replace them with RubyTimeObjIGotOutOfMyDb.in_time_zone(user_timezone)
where the user's timezone is a column in my user's table. What happened is now my page takes maybe 5 or 6 times as long to load. Is this the wrong strategy? Is there a better way I should be preparing for users in different timezones from my server?
问问题
108 次
1 回答
0
看这个railscast
我想你只需要做
控制器/应用程序.rb
before_filter :set_user_time_zone
private
def set_user_time_zone
Time.zone = current_user.time_zone if logged_in?
end
从 db 获取时,所有时间都将被转换
如果时间不在当前时区,请在视图中尝试(或在控制台中进行测试)
Time.now.in_time_zone
更新:如果您in_time_zone(zone)
多次调用该调用,我认为它会多次获取相应的时差,就像截屏视频所说的那样,它只会获取一次并在每次转换中使用它。
于 2012-05-16T01:26:38.607 回答