2011-12-15 113 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' 

你的其他後需要。

相關問題