2016-09-19 33 views
0

我发送电子邮件给供应商的部分逻辑是承诺日期必须小于今天的日期才能发送电子邮件。无论出于何种原因,即使它应该是假的,它也是如此。我在控制台中测试它,它显示错误,但它发送电子邮件...所以我猜测它传递的是真实的。时间转换Ruby on Rails 4不能正常工作

def self.send_vendor_openorder_notification(user, vendor) 
    po_collection = Array.new 
    user.purchase_orders.where({open_order: true, vendor_number: vendor.vendor_number}).where.not(email_last_sent: Time.now.midnight).each do |x| 
     if x.promise_date == nil || DateTime.strptime(x.promise_date, '%m/%d/%y') + 7.hours < Time.now.midnight 
     if x.email_sent == false 
      po_collection << x.id 
      x.email_sent = true 
      x.email_last_sent = Time.now.midnight 
      x.save 
     end 
     end 
    end 
    if po_collection.empty? == false 
     VendorSender.open_order_sender(user, vendor, po_collection).deliver_later 
    end 
    end 

在这里,如果promise_date在零点之前是零或小于今天,它应该通过。然而,“09/19/2016”的承诺日期在2016年9月19日正确通过。在heroku控制台(这是通过Heroku部署),这是错误的...我在这里错过了什么?

irb(main):038:0> DateTime.strptime("09/19/16", '%m/%d/%y') + 7.hours < Time.now.midnight 
=> false 
irb(main):039:0> DateTime.strptime("09/19/16", '%m/%d/%y') + 7.hours 
=> Mon, 19 Sep 2016 07:00:00 +0000 
irb(main):040:0> Time.now.midnight 
=> 2016-09-19 00:00:00 -0700 
irb(main):041:0> 
+0

它返回false,因为两个日期完全相同,并且您询问Time.now.midnight是否低于DateTime.strptime。 – luissimo

+0

@luissimo没错,日期是一样的,但有不同的时区。 –

+0

我希望它返回false。问题是它使用.zone工作返回true – Doughtz

回答

3

我认为问题出在你的TimeZone上。

始终使用Time.zone.now而不是Time.nowClick here了解更多详情。

您可以像这样更改您的if条件。

x.promise_date == nil || (Time.zone.parse(x.promise_date.strftime('%Y-%m-%d'))) + 7.hours < Time.zone.now.midnight 

希望这会有所帮助。

+0

。 – Doughtz