2012-03-16 124 views
4

我有一個在Rails服務器上運行的cron作業。當某些事件觸發時,這個耙子任務使用戶/ SMS發送給訂戶。現在,當這個事件在晚上10點到8點之間觸發時,我想將它存儲在隊列中,因爲沒有人喜歡在半夜中受到干擾。如何檢查時間是晚上10點至8點之間的時間。指定範圍之間的時間

注:每個用戶都有自己的時區..這我已經在數據庫

感謝

+0

目前我正在使用檢查一個包含所有小時之間的數組。即 'hour_array =%(重量)(22 23 0 1 2 3 4 5 6 7)' 'store_in_queue如果hour_array.include?(Time.now.utc.hour.to_s)' 是否有更好的方法來做這個? – benchwarmer 2012-03-16 14:32:47

回答

3

也許像...

def night_time?(t) 
    t.hour > 22 || t.hour < 8 
end 

可用於:

store_in(queue) if not night_time?(@time) 
5
# here "-07:00" is example of user's timezone offset 
if Time.now.getlocal("-07:00").hour.between?(8, 22) 
    #then we send 
else 
    #we don't send 
end