4

Rails 将所有DateTimes 存储在 UTC 时区中。我现在需要在他们的浏览器报告他们所在的时区中向用户显示那个时间。有没有简单的方法可以做到这一点?

使用最新的 Rails (3.2.13)。

4

2 回答 2

7

有人将解决方案变成了宝石:

https://github.com/basecamp/local_time

于 2014-11-20T01:40:54.013 回答
2

在客户端(js)中:

function set_time_zone_offset() {
    var current_time = new Date();
    $.cookie('time_zone', current_time.getTimezoneOffset());
}

在应用程序控制器中:

before_filter :set_timezone 

def set_timezone  
 min = request.cookies["time_zone"].to_i
 Time.zone = ActiveSupport::TimeZone[-min.minutes]
end 

信用:https ://stackoverflow.com/a/5930596/643500

于 2013-03-15T18:10:28.230 回答