2012-03-16 130 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