0

指示时间范围的 params 字符串

sched = "8:00AM-5:30PM"

我的数据库中存储了开始和结束时间

client1.sched_start    # => 2000-01-01 07:30:00 UTC
client1.sched_end      # => 2000-01-01 17:30:00 UTC
client2.sched_start    # => 2000-01-01 08:30:00 UTC
client2.sched_end      # => 2000-01-01 16:30:00 UTC

如何测试客户端时间范围是否在计划的时间范围内,返回 true 或 false?

到目前为止,我有这个,但我总是得到正确的和弃用警告,而且它真的很慢。

time_open = Time.use_zone('UTC'){Time.zone.parse '2000-01-01 '+sched.split("-")[0]}
time_close = Time.use_zone('UTC'){Time.zone.parse '2000-01-01 '+sched.split("-")[1]}
range = time_open..time_close
range === client1.sched_start && range === client1.sched_end
range === client2.sched_start && range === client2.sched_end

这是警告的摘录

warning: Time#succ is obsolete; use time + 1

此外,时间是不同的格式。例如,

time_open # Sat, 01 Jan 2000 08:00:00 UTC 00:00

到目前为止,这些都是有帮助的:

4

1 回答 1

0

让我们简化您的问题,看看我们是否可以让您走上正确的道路。 time_open评估为以下值:

>> time_open = Time.use_zone('UTC'){Time.zone.parse '2000-01-01 ' + "8:00AM"}
=> Sat, 01 Jan 2000 08:00:00 UTC +00:00

让我们将client1.sched_start(我将其称为)转换为 Time 对象(如果已经是 Time 对象,scheduled_time则可能不需要此步骤,请检查)。client1.sched_startclient1.sched_start.class

>> scheduled_time = Time.parse('2000-01-01 07:30:00 UTC')
=> 2000-01-01 07:30:00 UTC

time_open并且scheduled_time不相等:

>> time_open == scheduled_time
=> false

如果我们比较time_open同一时间,它将相等:

>> correct_start = Time.parse('2000-01-01 08:00:00 UTC')
=> 2000-01-01 08:00:00 UTC
>> time_open == correct_start
=> true
  • 编辑 - 如何检查时间是否在一个范围内 *

<我认为使用and运算符检查时间是否在范围内会更清楚>

>> start_time = Time.new(2013, 7, 29, 10)
=> 2013-07-29 10:00:00 -0400
>> end_time = Time.new(2013, 7, 29, 16)
=> 2013-07-29 16:00:00 -0400
>> subject_time = Time.new(2013, 7, 29, 14)
=> 2013-07-29 14:00:00 -0400
>> subject_time > start_time && subject_time < end_time
=> true
于 2013-07-24T23:54:51.980 回答