如何通过用户选择开始时间,TimeZOne,结束时间来实施免打扰(DND)

时间:2012-04-17 11:50:50

标签: ruby-on-rails ruby ruby-on-rails-3 time drag-and-drop

我在Rails服务器上运行了一个cron作业。当某些事件触发时,此rake任务会向订阅者发出呼叫/ SMS。现在,当此事件在用户选择的DND时间之间触发时,我想将其存储在队列中。如何检查时间是否在用户选择的DND时间之间。

我尝试在Time between specified range

中回答提及

但是当用户选择DND Time为6到9时,它会失败

Note:- 1] Every user has its own timezone.. Which I have in the database
2] Every user can select his/her own DND Time.

1 个答案:

答案 0 :(得分:2)

我认为以下方法有效 如果

start_hour = Hour when DND Time Starts
end_hour = Hour when DND Time Ends
current hour = Hour of the current time

然后

if start_hour > end_hour
  current hour > start_hour || current hour < end_hour
else
  current hour > start_hour && current hour < end_hour
end

OR

start_hour > end_hour ?  (current hour > start_hour || current hour < end_hour) : 
                         (current hour > start_hour && current hour < end_hour)