2011-12-15 112 views
0

我的Ruby脚本中有一些代码行,它获取当前日期(我的格林威治标准时间)并将其转换为ET(东部时间)。未初始化的常量ActiveSupport :: TimeZone(NameError)

我有这样的代码在我的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

+0

你是否已经要求'rubygems'并且在代码顶部需要'active_support'?如果你不需要,你需要它。 – ctcherry 2011-12-15 17:28:03

+0

是的,我有这些要求在我的代码。仍然不起作用。 – swiftcode 2011-12-15 17:29:53

回答

1

添加

require 'active_support/time_with_zone' 

你的其他后需要。

相关问题