在 Rails 中设置当前 Time.zone? [英] Set current Time.zone in Rails?

查看:43
本文介绍了在 Rails 中设置当前 Time.zone?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我页面上的 Javascript 将客户端 UTC 偏移量保存到 cookie.如何使用此 cookie 创建 TimeZone 并将其分配给 Time.zone ?

Javascript on my page saves client UTC offset to the cookie. How do I use this cookie to create a TimeZone and assign it to Time.zone ?

我需要类似的东西:

before_filter :set_time_zone

def set_time_zone
  Time.zone = ActiveSupport::TimeZone.new('my timezone', cookies[:timezone])
end

除了这个表达式的正确部分不起作用,我不确定我是否在这里走正确的路.拿不到.

except that the right part of this expression does not work and I'm not sure if I'm going the right way here. Can't get it.

推荐答案

这是谷歌搜索的有效答案:

Here's the working googled answer:

min = cookies[:timezone].to_i
Time.zone = ActiveSupport::TimeZone[-min.minutes]

为了清楚起见,javascript部分:

Just to make it clear, the javascript part:

if(!($.cookie('timezone'))) {
  current_time = new Date();
  $.cookie('timezone', current_time.getTimezoneOffset(), { path: '/', expires: 10 } );
} 

这篇关于在 Rails 中设置当前 Time.zone?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆