如何在ActiveSupport的TimeZone类中设置时区

我试图将文本解析为使用ruby中的慢性日期。 但是,在尝试解析字符串时,我一直遇到错误。

错误: NoMethodError - undefined method 'zone=' for Time:Class:

这是代码:

 require 'rubygems' require 'sinatra' require 'chronic' require 'date' require 'time' require 'active_support' require 'cgi' require 'json' module TimeAPI ZoneOffset = { 'A' => +1, 'ADT' => -3, 'AKDT' => -8, 'AKST' => -9, 'AST' => -4, 'B' => +2, 'BST' => +1, 'C' => +3, 'CDT' => -5, 'CEDT' => +2, 'CEST' => +2, 'CET' => +1, 'CST' => -6, 'D' => +4, 'E' => +5, 'EDT' => -4, 'EEDT' => +3, 'EEST' => +3, 'EET' => +2, 'EST' => -5, 'F' => +6, 'G' => +7, 'GMT' => 0, 'H' => +8, 'HADT' => -9, 'HAST' => -10, 'I' => +9, 'IST' => +1, 'K' => +10, 'L' => +11, 'M' => +12, 'MDT' => -6, 'MSD' => +4, 'MSK' => +3, 'MST' => -7, 'N' => -1, 'O' => -2, 'P' => -3, 'PDT' => -7, 'PST' => -8, 'Q' => -4, 'R' => -5, 'S' => -6, 'T' => -7, 'U' => -8, 'UTC' => 0, 'V' => -9, 'W' => -10, 'WEDT' => +1, 'WEST' => +1, 'WET' => 0, 'X' => -11, 'Y' => -12, 'Z' => 0 } class App  time }.to_json : time time = jsonp? ? callback + '(' + time + ');' : time end end end class Time def to_datetime # Convert seconds + microseconds into a fractional number of seconds seconds = sec + Rational(usec, 10**6) # Convert a UTC offset measured in minutes to one measured in a # fraction of a day. offset = Rational(utc_offset, 60 * 60 * 24) DateTime.new(year, month, day, hour, min, seconds, offset) end end class DateTime def to_datetime self end def to_s(format='') unless format.empty? strftime(format) else strftime end end end 

当我尝试使用以下设置时区时:

 Time.zone = offset Chronic.time_class = Time.zone 

它抛出了我的错误。 我打印偏移量,根据文档是合适的。 我甚至用'Eastern Time (US & Canada)'替换了计算的偏移量(直接来自ActiveSupport Docs )

错误: NoMethodError - undefined method 'now' for "UTC":String:

“现在”是需要解析的时间,“UTC”是时区。

任何和所有帮助真的很感激。

代码来源: timeapi

你能尝试require 'active_support/all'吗?

 irb(main):020:0> require 'active_support/all' => true irb(main):021:0> Time.zone => nil irb(main):022:0> Time.zone = "UTC" => "UTC" irb(main):023:0> Time.zone => #, @current_period=#>>> irb(main):024:0> Chronic.time_class = Time.zone => #, @current_period=#>>> 

看起来像NoMethodError - undefined method 'zone=' for Time:Class:已经消失了。