【发布时间】: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' ?如果你不需要,你需要它。
-
是的,我的代码中有这些要求。还是不行。