Prezes Łukasz
Prezes Łukasz

Reputation: 968

Issue with checking time ranges overlapping

Issue: overlaps? method doesn't return expect value.

(start_on.to_i..end_on.to_i).overlaps?(ti.start_time.to_i..ti.end_time.to_i)

It returns false, but should be true.

start_on: 2016-08-19 11:00:00 +0200

end_on: 2016-08-19 12:00:00 +0200

ti.start_time: 2000-01-01 08:00:00 UTC

ti.end_time: 2000-01-01 12:00:00 UTC

Hours like 11:00-12:00 overlaps with 08:00-12:00. Why method returns false? All columns in database are type of time. Current date is caused by Time.parse method.

I suppose that problem is with date second part of times have 2000 year, but first 2016. Anyone know how to fix it?

Upvotes: 0

Views: 38

Answers (1)

Tomek Wejchorowski
Tomek Wejchorowski

Reputation: 58

You can try by getting only time by this strftime("%H:%M"). Result:

(start_on.strftime("%H:%M")..end_on.strftime("%H:%M")).overlaps?(ti.start_time.strftime("%H:%M")..ti.end_time.strftime("%H:%M"))

Upvotes: 1

Related Questions