Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在ActiveSupport';s时区类_Ruby On Rails_Ruby_Datetime_Undefined_Chronic - Fatal编程技术网

Ruby on rails 如何在ActiveSupport';s时区类

Ruby on rails 如何在ActiveSupport';s时区类,ruby-on-rails,ruby,datetime,undefined,chronic,Ruby On Rails,Ruby,Datetime,Undefined,Chronic,我试图在ruby中使用chronic将文本解析为日期。但是,我在尝试解析字符串时一直遇到错误 错误:NoMethodError-未定义时间:类的方法“zone=” 代码如下: require 'rubygems' require 'sinatra' require 'chronic' require 'date' require 'time' require 'active_support' require 'cgi' require 'json' module TimeAPI ZoneO

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

错误:
NoMethodError-未定义时间:类的方法“zone=”

代码如下:

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 < Sinatra::Base

    set :sessions, false
    set :run, false
    set :environment, ENV['RACK_ENV']

    def callback
      (request.params['callback'] || '').gsub(/[^a-zA-Z0-9_]/, '')
    end

    def prefers_json?
      (request.accept.first || '').downcase == 'application/json'
    end

    def json?
      prefers_json? \
        || /\.json$/.match((params[:zone] || '').downcase) \
        || /\.json$/.match((params[:time] || '').downcase)
    end

    def jsonp?
      json? && callback.present?
    end

    def format
      format = (request.params.select { |k,v| v.blank? }.first || [nil]).first \
        || request.params['format'] \
        || (jsonp? ? '%B %d, %Y %H:%M:%S GMT%z' : '')
      CGI.unescape(format).gsub('\\', '%')
    end

    get '/' do
      erb :index
    end

    get '/favicon.ico' do
      ''
    end

    get '/:zone/?' do
      parse(params[:zone])
    end

    get '/:zone/:time/?' do
      parse(params[:zone], params[:time])
    end

    def parse(zone='UTC', time='now')
      zone = zone.gsub(/\.json$/, '').upcase
      puts zone
      offset = ZoneOffset[zone] || Integer(zone)
      puts offset
      time = time \
        .gsub(/\.json$/, '') \
        .gsub(/^at /, '') \
        .gsub(/(\d)h/, '\1 hours') \
        .gsub(/(\d)min/, '\1 minutes') \
        .gsub(/(\d)m/, '\1 minutes') \
        .gsub(/(\d)sec/, '\1 seconds') \
        .gsub(/(\d)s/, '\1 seconds')

      puts time

      if prefers_json?
        response.headers['Content-Type'] = 'application/json'
      end
      Time.zone = offset
      Chronic.time_class = Time.zone
      time = Chronic.parse(time).to_datetime.to_s(format)
      time = json? ? { 'dateString' => 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
这让我犯了那个错误。我正在打印胶印版,这是适当的根据。我甚至用
“东部时间(美国和加拿大)”
(直接从)

错误:
NoMethodError-未定义“UTC”的方法“现在”:字符串:

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

我们真诚地感谢您的任何帮助


代码来源:

您能否尝试要求“主动支持/全部”

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
=> #<ActiveSupport::TimeZone:0x007fd859e31910 @name="UTC", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Etc/UTC>, @current_period=#<TZInfo::TimezonePeriod: nil,nil,#<TZInfo::TimezoneOffset: 0,0,UTC>>>>
irb(main):024:0> Chronic.time_class = Time.zone
=> #<ActiveSupport::TimeZone:0x007fd859e31910 @name="UTC", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Etc/UTC>, @current_period=#<TZInfo::TimezonePeriod: nil,nil,#<TZInfo::TimezoneOffset: 0,0,UTC>>>>
irb(主):020:0>需要“主动支持/全部”
=>正确
irb(主):021:0>时区
=>零
irb(主):022:0>Time.zone=“UTC”
=>“UTC”
irb(主):023:0>时区
=> #>
irb(主):024:0>Chronic.time_class=time.zone
=> #>
看起来,Time:Class:的未定义方法“zone=”消失了

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
=> #<ActiveSupport::TimeZone:0x007fd859e31910 @name="UTC", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Etc/UTC>, @current_period=#<TZInfo::TimezonePeriod: nil,nil,#<TZInfo::TimezoneOffset: 0,0,UTC>>>>
irb(main):024:0> Chronic.time_class = Time.zone
=> #<ActiveSupport::TimeZone:0x007fd859e31910 @name="UTC", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Etc/UTC>, @current_period=#<TZInfo::TimezonePeriod: nil,nil,#<TZInfo::TimezoneOffset: 0,0,UTC>>>>