简体   繁体   English

Ruby Newbie strftime Time.Now

[英]Ruby Newbie strftime Time.Now

I have the following written in ruby 我用红宝石写了以下内容

t = Time.now
    t.strftime("%Y-%d-%m")

SCHEDULER.every '1m', :first_in => 0 do |job|
  send_event('gmail_gh', {current: gmail.inbox.count(:after => Date.parse(t)), older: gmail.inbox.count})  

But i get this error 但是我得到了这个错误

scheduler caught exception:
can't convert Time into String
/var/dashing/cdmdash/jobs/gmail_gh.rb:21:in `parse'
/var/dashing/cdmdash/jobs/gmail_gh.rb:21:in `block in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rufus-scheduler-2.0.17/lib/rufus/sc/jobs.rb:231:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rufus-scheduler-2.0.17/lib/rufus/sc/jobs.rb:231:in `trigger_block'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rufus-scheduler-2.0.17/lib/rufus/sc/jobs.rb:191:in `block in trigger'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rufus-scheduler-2.0.17/lib/rufus/sc/scheduler.rb:416:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/rufus-scheduler-2.0.17/lib/rufus/sc/scheduler.rb:416:in `block in trigger_job'

I think it has something to do with the t variable and it not being a truing, I am new to Ruby so I am abit stuck 我认为它与t变量有关,它不是一个修正,我是Ruby的新手,所以我很高兴卡住了

If you look at the gem documentation, you will see that the :after and :before params take in a date in the format of YYYY-MM-DD. 如果你查看gem文档,你会看到:after和:params之前采用YYYY-MM-DD格式的日期。

From the gem Readme: 来自宝石自述文件:

gmail.inbox.count(:after => Date.parse("2010-02-20"), :before => Date.parse("2010-03-20"))
gmail.inbox.count(:on => Date.parse("2010-04-15"))

Your code is passing in YYYY-DD-MM which is likely causing the error. 您的代码传递的是YYYY-DD-MM,这可能会导致错误。

Edit 编辑

When you call strftime on a datetime object, it doesn't modify the object - only returns the string notation based on format you give. 在datetime对象上调用strftime时,它不会修改对象 - 仅根据您提供的格式返回字符串表示法。

As a result, the Date.parse(t) is still getting Time.now was a parameter. 结果,Date.parse(t)仍然是Time.now是一个参数。

Try this: 尝试这个:

t = Time.now.strftime("%Y-%m-%d")
Date.parse(t)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM