【问题标题】:Uninitialized constant ActiveSupport::TimeZone (NameError)未初始化的常量 ActiveSupport::TimeZone (NameError)
【发布时间】:2012-01-21 09:18:01
【问题描述】:

我的 Ruby 脚本中有几行代码可以获取当前日期(我的是 GMT)并将其转换为 ET(东部时间)。

我的 Ruby 脚本中有这段代码:

# get current time and date in ET
my_offset = 3600 * -5  # US Eastern

# find the zone with that offset
zone_name = ActiveSupport::TimeZone::MAPPING.keys.find do |name|
  ActiveSupport::TimeZone[name].utc_offset == my_offset
end
zone = ActiveSupport::TimeZone[zone_name]

time_locally = Time.now
time_in_zone = zone.at(time_locally)

问题是它在这里给出了一个错误(好吧,在这一行):zone_name = ActiveSupport::TimeZone::MAPPING.keys.find do |name|uninitialized constant ActiveSupport::TimeZone (NameError)

有人知道怎么回事吗?我从 Stack Overflow 获得了这段代码,here

【问题讨论】:

  • 你在代码顶部是否有 require 'rubygems' 和 require 'active_support' ?如果你不需要,你需要它。
  • 是的,我的代码中有这些要求。还是不行。

标签: ruby timezone


【解决方案1】:

添加

require 'active_support/time_with_zone'

根据您的其他要求。

【讨论】:

  • 解决了这个错误,但现在它给出了一个新错误:/Library/Ruby/Gems/1.8/gems/activesupport-3.0.0/lib/active_support/values/time_zone.rb:260:in at': undefined method in_time_zone' for Thu Dec 15 17:43:18 UTC 2011:Time (NoMethodError) from ruby ratings.rb:35 我的代码中的第 35 行是:time_in_zone = zone.at(time_locally)
猜你喜欢
  • 1970-01-01
  • 2011-07-07
  • 2022-11-03
  • 1970-01-01
  • 1970-01-01
  • 2011-10-14
  • 1970-01-01
  • 2015-01-19
  • 2015-10-29
相关资源
最近更新 更多