新手问题 从 API 获取字符串类型的时间,然后保存到数据库!

stephen · 2012年12月15日 · 最后由 stephen 回复于 2012年12月16日 · 3267 次阅读

我用 sidekiq 从一个 API 获取到两个字段,都是字符串。 一个是日期 "2012-09-09" 一个是时间"13:34:09" 然后我用

DateTime.strptime("#{@records[30]} #{@records[31]}", '%Y-%m-%d %H:%M:%S')

或者

DateTime.parse("#{@records[30]} #{@records[31]}")

转换成时间格式,然后保存到数据库,

class xxx
  field :got_at, :type => DateTime
end

但是都出现这个错误

012-12-15T12:27:44Z 1077 TID-owuatkq7w StockspiderWorker MSG-owua5ch64 INFO: fail: 0.633 sec
2012-12-15T12:27:44Z 1077 TID-owuatkq7w WARN: invalid date
2012-12-15T12:27:44Z 1077 TID-owuatkq7w WARN: /Users/stephen/Gupiao/app/workers/stockspider_worker.rb:17:in `strptime'
/Users/stephen/Gupiao/app/workers/stockspider_worker.rb:17:in `block in perform'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-3.0.1/lib/mongoid/contextual/mongo.rb:672:in `yield_and_increment'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-3.0.1/lib/mongoid/contextual/mongo.rb:134:in `block (2 levels) in each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/moped-1.1.6/lib/moped/query.rb:78:in `block in each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/moped-1.1.6/lib/moped/cursor.rb:26:in `block in each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/moped-1.1.6/lib/moped/cursor.rb:26:in `each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/moped-1.1.6/lib/moped/cursor.rb:26:in `each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/moped-1.1.6/lib/moped/query.rb:77:in `each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/moped-1.1.6/lib/moped/query.rb:77:in `each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-3.0.1/lib/mongoid/contextual/mongo.rb:133:in `block in each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-3.0.1/lib/mongoid/contextual/mongo.rb:632:in `selecting'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-3.0.1/lib/mongoid/contextual/mongo.rb:132:in `each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-3.0.1/lib/mongoid/contextual.rb:18:in `each'
/Users/stephen/Gupiao/app/workers/stockspider_worker.rb:8:in `perform'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/processor.rb:36:in `block (3 levels) in process'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:75:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:75:in `block in invoke'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/server/timeout.rb:14:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:77:in `block in invoke'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/server/active_record.rb:6:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:77:in `block in invoke'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/server/retry_jobs.rb:30:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:77:in `block in invoke'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/server/logging.rb:11:in `block in call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/logging.rb:22:in `with_context'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/server/logging.rb:7:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:77:in `block in invoke'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/server/exception_handler.rb:9:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:77:in `block in invoke'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:80:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:80:in `invoke'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/processor.rb:35:in `block (2 levels) in process'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/processor.rb:66:in `stats'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/processor.rb:34:in `block in process'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/celluloid-0.10.0/lib/celluloid/calls.rb:56:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/celluloid-0.10.0/lib/celluloid/calls.rb:56:in `dispatch'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/celluloid-0.10.0/lib/celluloid/future.rb:18:in `block in initialize'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/celluloid-0.10.0/lib/celluloid/thread_pool.rb:46:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/celluloid-0.10.0/lib/celluloid/thread_pool.rb:46:in `block in create'

但是在 rails c 运行,没有出现这个错误。 应该要怎么解决?

2012-12-15T12:27:44Z 1077 TID-owuatkq7w WARN: invalid date
2012-12-15T12:27:44Z 1077 TID-owuatkq7w WARN: /Users/stephen/Gupiao/app/workers/stockspider_worker.rb:17:in `strptime'
/Users/stephen/Gupiao/app/workers/stockspider_worker.rb:17:in `block in perform'

严重怀疑你到 date 格式不正确。可以把 date 打出来。

@hooopo

1.9.3p125 :006 > DateTime.parse("#{@records[30]} #{@records[31]}")
 => Fri, 14 Dec 2012 15:03:07 +0000 
1.9.3p125 :005 > DateTime.strptime("#{@records[30]} #{@records[31]}", '%Y-%m-%d %H:%M:%S')
 => Fri, 14 Dec 2012 15:03:07 +0000 

#2 楼 @stephen No. 把你报错时候的 strptime 的参数打出来..

@stephen 在 sidekiq 的 worker 中用 puts 打印出来吗?好似打印不了!2 楼的结构就是我把 worker 里的代码在 rails c 中执行一次的结果,应该一样的吧?

#4 楼 @stephen 打到 log 里啊 这么简单的调试工作都懒得做还查什么问题呢

@hooopo

Rails.logger.info("#{@records[30]} #{@records[31]}")
Rails.logger.info(DateTime.strptime("#{@records[30]} #{@records[31]}", '%Y-%m-%d %H:%M:%S'))

结果分别是

2012-12-14 15:05:52
2012-12-14T15:05:52+00:00

你 Rails.logger 在哪里插入的?在这里? /Users/stephen/Gupiao/app/workers/stockspider_worker.rb:17

@hooopo 恩恩,是的,因为是这里调用了。

你调用DateTime.strptime("#{@records[30]} #{@records[31]}", '%Y-%m-%d %H:%M:%S')的时候都打印出时间了呀 没有错误呢

@hooopo 是的,所以我想不明白,看他报错,应该是 date 的参数验证错误,但是搞不明白!

直接用Time.parse

@hooopo 有 date,应该不能用 time,试了试,报错

2012-12-15T14:06:44Z 2229 TID-oxdpfwkik WARN: no time information in " "
2012-12-15T14:06:44Z 2229 TID-oxdpfwkik WARN: /Users/stephen/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/time.rb:267:in `parse'

@hooopo 用 DateTime.parse 也和 strptime 一样

1.9.3p327 :010 > DateTime.parse(" ")
ArgumentError: invalid date
    from (irb):10:in `parse'
    from (irb):10
    from /home/hooopo/.rvm/rubies/ruby-1.9.3-p327/bin/irb:16:in `<main>'
1.9.3p327 :011 > Time.parse " "
ArgumentError: no time information in " "
    from /home/hooopo/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/time.rb:267:in `parse'
    from (irb):11
    from /home/hooopo/.rvm/rubies/ruby-1.9.3-p327/bin/irb:16:in `<main>'
1.9.3p327 :012 > 

根据你的错误信息,你这两个错误都是因为传的是空字符串导致的。

你 worker 里到代码是怎么写的? /Users/stephen/Gupiao/app/workers/stockspider_worker.rb:17 这里出错那个方法

@hooopo

# encoding: utf-8
require 'net/http'
require 'iconv'

class StockspiderWorker
  include Sidekiq::Worker

  def perform
    Stock.all.each do |stock|
      @s = Net::HTTP.get(URI.parse("http://hq.sinajs.cn/list=" + stock[:market] + stock[:code]))
      @s = @s[@s.index("\"")+1, @s.rindex("\"")-1]
      @records = @s.split(",")
      stock.update_attributes(:name => Iconv.iconv("UTF-8", "GBK", @records[0]).first.to_s, 
        :volume => @records[8], :obv => @records[9], 
        :op => @records[1], :ycp => @records[2], 
        :cp => @records[3], :tp => @records[4], 
        :lp => @records[5], 
        :got_at => DateTime.parse("#{@records[30]}#{@records[31]}"))
    end
  end
end
class StockspiderWorker
  include Sidekiq::Worker

  def perform
    Stock.all.each do |stock|
      @s = Net::HTTP.get(URI.parse("http://hq.sinajs.cn/list=" + stock[:market] + stock[:code]))
      @s = @s[@s.index("\"")+1, @s.rindex("\"")-1]
      @records = @s.split(",")
      got_at = DateTime.parse("#{@records[30]}#{@records[31]}")
      Rails.logger.info(got_at)
      Rails.logger.info "*" * 100
      stock.update_attributes(:name => Iconv.iconv("UTF-8", "GBK", @records[0]).first.to_s, 
        :volume => @records[8], :obv => @records[9], 
        :op => @records[1], :ycp => @records[2], 
        :cp => @records[3], :tp => @records[4], 
        :lp => @records[5], 
        :got_at => got_at)
    end
  end
end

这样试试,看看日志输出

@hooopo 输出不了,因为在 parse 那里已经错误,没有执行 log

好吧,这样!

class StockspiderWorker
  include Sidekiq::Worker

  def perform
    Stock.all.each do |stock|
      @s = Net::HTTP.get(URI.parse("http://hq.sinajs.cn/list=" + stock[:market] + stock[:code]))
      @s = @s[@s.index("\"")+1, @s.rindex("\"")-1]
      @records = @s.split(",")
      str_got_at = "#{@records[30]}#{@records[31]}"
      Rails.logger.info(str_got_at)
      Rails.logger.info "*" * 100
      got_at = DateTime.parse(str_got_at)

      stock.update_attributes(:name => Iconv.iconv("UTF-8", "GBK", @records[0]).first.to_s, 
        :volume => @records[8], :obv => @records[9], 
        :op => @records[1], :ycp => @records[2], 
        :cp => @records[3], :tp => @records[4], 
        :lp => @records[5], 
        :got_at => got_at)
    end
  end
end

@hooopo 呵呵,我也这样做了,这是输入结果!

2012-12-1415:05:52
****************************************************************************************************

中间少个空格吧 应该是2012-12-14 15:05:52

@hooopo 我原来也是有个空格的,但是 14 楼说是空白造成,所以,我去掉了空格

#22 楼 @stephen 。。。。。。你加上试试 看日志和异常

@hooopo

2012-12-14 15:05:52
****************************************************************************************************

异常

2012-12-15T17:53:28Z 4184 TID-ovrw0mpy0 WARN: invalid date
2012-12-15T17:53:28Z 4184 TID-ovrw0mpy0 WARN: /Users/stephen/Gupiao/app/workers/stockspider_worker.rb:16:in `parse'
/Users/stephen/Gupiao/app/workers/stockspider_worker.rb:16:in `block in perform'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-3.0.1/lib/mongoid/contextual/mongo.rb:672:in `yield_and_increment'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-3.0.1/lib/mongoid/contextual/mongo.rb:134:in `block (2 levels) in each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/moped-1.1.6/lib/moped/query.rb:78:in `block in each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/moped-1.1.6/lib/moped/cursor.rb:26:in `block in each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/moped-1.1.6/lib/moped/cursor.rb:26:in `each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/moped-1.1.6/lib/moped/cursor.rb:26:in `each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/moped-1.1.6/lib/moped/query.rb:77:in `each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/moped-1.1.6/lib/moped/query.rb:77:in `each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-3.0.1/lib/mongoid/contextual/mongo.rb:133:in `block in each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-3.0.1/lib/mongoid/contextual/mongo.rb:632:in `selecting'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-3.0.1/lib/mongoid/contextual/mongo.rb:132:in `each'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/mongoid-3.0.1/lib/mongoid/contextual.rb:18:in `each'
/Users/stephen/Gupiao/app/workers/stockspider_worker.rb:9:in `perform'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/processor.rb:36:in `block (3 levels) in process'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:75:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:75:in `block in invoke'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/server/timeout.rb:14:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:77:in `block in invoke'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/server/active_record.rb:6:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:77:in `block in invoke'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/server/retry_jobs.rb:30:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:77:in `block in invoke'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/server/logging.rb:11:in `block in call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/logging.rb:22:in `with_context'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/server/logging.rb:7:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:77:in `block in invoke'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/server/exception_handler.rb:9:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:77:in `block in invoke'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:80:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/middleware/chain.rb:80:in `invoke'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/processor.rb:35:in `block (2 levels) in process'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/processor.rb:66:in `stats'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/sidekiq-1.2.1/lib/sidekiq/processor.rb:34:in `block in process'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/celluloid-0.10.0/lib/celluloid/calls.rb:56:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/celluloid-0.10.0/lib/celluloid/calls.rb:56:in `dispatch'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/celluloid-0.10.0/lib/celluloid/future.rb:18:in `block in initialize'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/celluloid-0.10.0/lib/celluloid/thread_pool.rb:46:in `call'
/Users/stephen/.rvm/gems/ruby-1.9.3-p125/gems/celluloid-0.10.0/lib/celluloid/thread_pool.rb:46:in `block in create'

先打印 str_got_at.encoding 然后打印 str_got_at.bytes.to_a.inspect 然后用 DateTime.parse(Iconv.iconv("UTF-8", "GBK", str_got_at))

@hooopo

str_got_at = "#{@records[30]} #{@records[31]}"
      Rails.logger.info(str_got_at)
      Rails.logger.info(str_got_at.encoding)
      Rails.logger.info(str_got_at.bytes.to_a.inspect)
      Rails.logger.info "*" * 100
      got_at = DateTime.parse(Iconv.iconv("UTF-8", "GBK", str_got_at).first.to_s)
2012-12-14 15:05:52
ASCII-8BIT
[50, 48, 49, 50, 45, 49, 50, 45, 49, 52, 32, 49, 53, 58, 48, 53, 58, 53, 50]
2012-12-14 15:05:52
****************************************************************************************************
需要 登录 后方可回复, 如果你还没有账号请 注册新账号