2016-09-19 34 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