无法使用缩写设置时区
Can't set timezone using abbreviation
我无法使用其缩写在 Rails 上设置时区,例如:
>> Time.zone = 'BRT'
ArgumentError: Invalid Timezone: BRT
from /home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-3.2.21/lib/active_support/core_ext/time/zones.rb:61:in `rescue in find_zone!'
from /home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-3.2.21/lib/active_support/core_ext/time/zones.rb:53:in `find_zone!'
from /home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-3.2.21/lib/active_support/core_ext/time/zones.rb:37:in `zone='
from (irb):14
from /home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-3.2.21/lib/rails/commands/console.rb:47:in `start'
from /home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-3.2.21/lib/rails/commands/console.rb:8:in `start'
from /home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-3.2.21/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
这是必要的,因为某些系统(android 和某些浏览器)使用缩写报告时区。
缩写列表可以在 http://en.wikipedia.org/wiki/List_of_time_zone_abbreviations
找到
jstimezone 正在使用缩写报告时区。它也有很多错误且无人维护 (https://bitbucket.org/pellepim/jstimezonedetect/issues?status=new&status=open)。使用标准 javascript:
更简单
var offset = - new Date().getTimezoneOffset()/60
然后调用文档准备好:
$.cookie("browser.tzoffset", offset, { expires: 30, path: '/' })
然后在rails中使用around_filter
在ApplicationController
中:
def set_time_zone
return yield unless (utc_offset = cookies['browser.tzoffset']).present?
utc_offset = utc_offset.to_i
gmt_offset = if utc_offset == 0 then nil elsif utc_offset > 0 then -utc_offset else "+#{-utc_offset}" end
Time.use_zone("Etc/GMT#{gmt_offset}"){ yield }
rescue ArgumentError
yield
end
这会为用户本地化所有日期,独立于 he/she 所在的位置。例如,在巴西,我们有多个时区。
PS:ActiveSupport::TimeZone[utc_offset.to_i]
不能使用,因为它使用 DST,请参阅 https://github.com/rails/rails/issues/20504
PS:也可以使用矩:moment.parseZone(Date.now()).utcOffset()/60
或moment().format('zz')
您不必使用 around_filter。
把这个放在 before_action
Time.zone = "Etc/GMT#{gmt_offset}"
(Time.zone是线程本地的,可以安全更改。)
我无法使用其缩写在 Rails 上设置时区,例如:
>> Time.zone = 'BRT'
ArgumentError: Invalid Timezone: BRT
from /home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-3.2.21/lib/active_support/core_ext/time/zones.rb:61:in `rescue in find_zone!'
from /home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-3.2.21/lib/active_support/core_ext/time/zones.rb:53:in `find_zone!'
from /home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-3.2.21/lib/active_support/core_ext/time/zones.rb:37:in `zone='
from (irb):14
from /home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-3.2.21/lib/rails/commands/console.rb:47:in `start'
from /home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-3.2.21/lib/rails/commands/console.rb:8:in `start'
from /home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-3.2.21/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
这是必要的,因为某些系统(android 和某些浏览器)使用缩写报告时区。 缩写列表可以在 http://en.wikipedia.org/wiki/List_of_time_zone_abbreviations
找到jstimezone 正在使用缩写报告时区。它也有很多错误且无人维护 (https://bitbucket.org/pellepim/jstimezonedetect/issues?status=new&status=open)。使用标准 javascript:
更简单var offset = - new Date().getTimezoneOffset()/60
然后调用文档准备好:
$.cookie("browser.tzoffset", offset, { expires: 30, path: '/' })
然后在rails中使用around_filter
在ApplicationController
中:
def set_time_zone
return yield unless (utc_offset = cookies['browser.tzoffset']).present?
utc_offset = utc_offset.to_i
gmt_offset = if utc_offset == 0 then nil elsif utc_offset > 0 then -utc_offset else "+#{-utc_offset}" end
Time.use_zone("Etc/GMT#{gmt_offset}"){ yield }
rescue ArgumentError
yield
end
这会为用户本地化所有日期,独立于 he/she 所在的位置。例如,在巴西,我们有多个时区。
PS:ActiveSupport::TimeZone[utc_offset.to_i]
不能使用,因为它使用 DST,请参阅 https://github.com/rails/rails/issues/20504
PS:也可以使用矩:moment.parseZone(Date.now()).utcOffset()/60
或moment().format('zz')
您不必使用 around_filter。
把这个放在 before_action
Time.zone = "Etc/GMT#{gmt_offset}"
(Time.zone是线程本地的,可以安全更改。)