Ruby on rails Can';t使用缩写设置时区

Ruby on rails Can';t使用缩写设置时区,ruby-on-rails,timezone,activesupport,Ruby On Rails,Timezone,Activesupport,我无法使用其缩写在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_zon

我无法使用其缩写在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>'
>Time.zone='BRT'
ArgumentError:无效时区:BRT
from/home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-3.2.21/lib/active\u support/core\u ext/time/zones.rb:61:“在查找区域中进行救援!”
from/home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-3.2.21/lib/active\u support/core\u ext/time/zones.rb:53:in“find\u zone!”
from/home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-3.2.21/lib/active\u support/core\u ext/time/zones.rb:37:in`zone='1
来自(irb):14
来自/home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-3.2.21/lib/rails/commands/console.rb:47:在“开始”中
来自/home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-3.2.21/lib/rails/commands/console.rb:8:在“开始”中
来自/home/braulio/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-3.2.21/lib/rails/commands.rb:41:in`'
来自脚本/rails:6:in'require'
来自脚本/rails:6:in`'
这是必要的,因为一些系统(android和一些浏览器)使用缩写报告时区。
缩略语列表可在

jstimezone使用缩略语报告时区。它也有相当多的bug和未维护()。只使用标准javascript更简单:

var offset = - new Date().getTimezoneOffset()/60
然后调用文档就绪:

$.cookie("browser.tzoffset", offset, { expires: 30, path: '/' })
然后在rails中使用
应用程序控制器中的
环绕过滤器

  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
这会将用户的所有日期本地化,独立于用户所在的位置。例如,在巴西,我们有多个时区

PS:
ActiveSupport::时区[utc\u offset.to\u i]
无法使用,因为它使用DST,请参阅


PS:您还可以使用矩:
moment.parseZone(Date.now()).utcOffset()/60
moment().format('zz')
jstimezone使用缩写报告时区。它也有相当多的bug和未维护()。只使用标准javascript更简单:

var offset = - new Date().getTimezoneOffset()/60
然后调用文档就绪:

$.cookie("browser.tzoffset", offset, { expires: 30, path: '/' })
然后在rails中使用
应用程序控制器中的
环绕过滤器

  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
这会将用户的所有日期本地化,独立于用户所在的位置。例如,在巴西,我们有多个时区

PS:
ActiveSupport::时区[utc\u offset.to\u i]
无法使用,因为它使用DST,请参阅


PS:您还可以使用矩:
矩.parseZone(Date.now()).utcOffset()/60
矩().format('zz')
您不必使用around\u过滤器。 在采取行动之前将其放入


Time.zone=“Etc/GMT#{GMT_offset}”


(Time.zone是线程本地的。可以安全地更改。)

您不必使用around\u过滤器。 在采取行动之前将其放入


Time.zone=“Etc/GMT#{GMT_offset}”


(Time.zone是线程本地的。可以安全地更改。)

对于不明确的缩写,您希望发生什么?基本上,您应该避免使用缩写作为指定时区的方式。在JavaScript中有多种检测时区的方法,如果这对您有帮助的话……嘿,Jon,是的,jstz使用缩写来报告时区,请参阅。你推荐另一个图书馆吗?我建议你找一个不同的,是的。我没有任何具体的建议,但我知道有很多可用的。如果缩写不明确,你会希望发生什么?基本上,您应该避免使用缩写作为指定时区的方式。在JavaScript中有多种检测时区的方法,如果这对您有帮助的话……嘿,Jon,是的,jstz使用缩写来报告时区,请参阅。你推荐另一个图书馆吗?我建议你找一个不同的,是的。我没有任何具体的建议,但我知道有很多。嗯。。。jsTimeZoneDetect不返回缩写。它返回时区,例如
美国/纽约
。如果它返回一个缩写,您应该将其作为一个bug来提出。另外,使用
getTimeZoneOffset
不适合时区检测,因为它只返回从调用它的
Date
对象的偏移量。由于您是在
new Date()
上调用它,因此它将返回代码运行所在时区的当前偏移量。您不能只获取偏移量并将其映射回时区,因为许多时区共享相同的偏移量,并在一年中的不同时间使用它们。请参阅.matt中的“时区!=Offset”,这个答案中的重要一点是将数据库中的日期本地化为特定用户的演示文稿。所以用户的时区并不重要。也许你可以使用
Intl.DateTimeFormat().resolved.timezone
?@Andy
Intl.DateTimeFormat().resolved.timezone
在一些使用Chrome的android设备上返回时区缩写。Uhhh。。。jsTimeZoneDetect不返回缩写。它返回时区,例如
美国/纽约
。如果它返回一个缩写,您应该将其作为一个bug来提出。另外,使用
getTimeZoneOffset
不适合时区检测,因为它只返回从调用它的
Date
对象的偏移量。由于您是在
new Date()
上调用它,因此它将返回代码运行所在时区的当前偏移量。您不能只获取偏移量并将其映射回时区,因为许多时区共享相同的偏移量,并在一年中的不同时间使用它们。请参阅.matt中的“时区!=Offset”,这个答案中的重要一点是将数据库中的日期本地化为特定用户的演示文稿。所以用户的时区并不重要。也许你可以使用
Intl.DateTimeFormat().resolved.timezone
?@Andy
Intl.DateTimeFormat().resolved.timezone
在一些android设备上使用Chrome返回时区缩写。