0

The params string that indicates a time range

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

There are start and end times stored in my database

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

How do I test whether a client time range is in the scheduled hours range, returning true or false?

So far, I have this, but I get true always and deprecation warnings plus it's really slow.

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

Here's an excerpt the warning

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

Also, the times are different formats. For example,

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

These were helpful in getting thus far:

Community
  • 1
  • 1
JHo
  • 1,068
  • 1
  • 14
  • 29

1 Answers1

0

Let's simplify your problem and see if we can set you on the right path. time_open evaluates to the following value:

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

Let's convert client1.sched_start (I refer to this as scheduled_time) to a Time object (this step might not be necessary if client1.sched_start is already a Time object, check with client1.sched_start.class).

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

time_open and scheduled_time are not equal:

>> time_open == scheduled_time
=> false

If we compare time_open to the same time, it will be equal:

>> correct_start = Time.parse('2000-01-01 08:00:00 UTC')
=> 2000-01-01 08:00:00 UTC
>> time_open == correct_start
=> true
  • Edit - how to check if a time is in a range *

I think it is much clearer to check if a time is within a range by using the < and > operators:

>> 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
Powers
  • 18,150
  • 10
  • 103
  • 108
  • Thanks for the response. That explains why the times were showing as different time formats. When I did `time_open.class`, I got `ActiveSupport::TimeWithZone`. So I changed my open/close times to `time_open = Time.parse("2000-01-01 08:00AM"+" UTC")` and now they are both `Time` classes. But when I follow-through and make a time range and then see if my Time objects are inside that range. I get an error. `TypeError: can't iterate from Time` – JHo Jul 25 '13 at 02:13
  • @JHo - I updated my answer to show how I think you should check if a time is within a range of times. Please let me know if this works for you! – Powers Jul 29 '13 at 23:39
  • I like this solution. It works for arbitrary dates and easily converts strings. This reference was useful too: http://stackoverflow.com/questions/1609267/how-to-find-if-range-is-contained-in-an-array-of-ranges – JHo Jul 30 '13 at 19:05