我使用以下功能在3个半小时内获得订单:
def send_last_3hours_ordered_sku
time = k = 3.hours.ago.strftime("%Y-%m-%d").to_time + 5.hours + 30.minutes # for IST time
title = "Sku Ordered since (#{time.to_s})"
os = Order.where("completed_at > ?", time).where("state = 'complete'")
Notification.send_email_to_help({:title => title, :attachment_name => title+".csv", :attachment => Admin::OrderQueryController.make_sku_csv(os)}).deliver
end
def send_last_half_hour_ordered_sku
time = k = 0.5.hours.ago.strftime("%Y-%m-%d").to_time + 5.hours + 30.minutes # for IST time
title = "Sku Ordered since (#{time.to_s})"
os = Order.where("completed_at > ?", time).where("state = 'complete'")
Notification.send_email_to_help({:title => title, :attachment_name => title+".csv", :attachment => Admin::OrderQueryController.make_sku_csv(os)}).deliver
end
3小时的功能正常,但半小时的功能没有。而不是在过去半小时内完成的订单,它返回当天的所有订单。
我也尝试过使用:
#1.
time = k = 30.minutes.ago.strftime("%Y-%m-%d").to_time + 5.hours + 30.minutes # for IST time
#this gives the same result
#2.
time = k = 30.mins.ago.strftime("%Y-%m-%d").to_time + 5.hours + 30.minutes # for IST time
#This gives an error : undefined method `mins' for 30:Fixnum
答案 0 :(得分:2)
而不是,
time = k = 0.5.hours.ago.strftime("%Y-%m-%d").to_time + 5.hours + 30.minutes
使用
time = k = 30.minutes.ago.in_time_zone(TZInfo::Timezone.get('Asia/Kolkata'))
答案 1 :(得分:0)
经验法则是rails可以自己处理时区,但在实践中往往很棘手,有时DB不是utc等。经验法则就是这样简单:
Order.where("completed_at > ?", 30.minutes.ago.to_s(:db)).where(state: 'complete')
应该适合你。
如果您仍然错误的时间,您可以尝试本文中的解决方案 - (它可能有助于您了解rails中时区的工作情况)http://chris.finne.us/2011/09/23/rails-3-tosdb-when-database-time-is-not-utc/