How do you write something like that, which is open late at night and increases by 15% from 23:00 to 5:00 the next day? The story. I want to make a good judgment on the condition of "23:00 to 5:00 the next day".
If you try to compare by Time type, it is troublesome to enter the date information, so you can compare after digitizing only the time part with strftime
and to_i
.
def late_time?(time)
num = time.strftime('%H%M').to_i
[*0..459, *2300..2359].include?(num)
end
target_time = Time.zone.parse('2020/9/1 23:00')
p late_time?(target_time) #=> true
target_time = Time.zone.parse('2020/9/2 0:00')
p late_time?(target_time) #=> true
target_time = Time.zone.parse('2020/9/2 4:59')
p late_time?(target_time) #=> true
target_time = Time.zone.parse('2020/9/2 5:00')
p late_time?(target_time) #=> false
target_time = Time.zone.parse('2020/9/2 6:00')
p late_time?(target_time) #=> false
target_time = Time.zone.parse('2020/9/2 22:59')
p late_time?(target_time) #=> false
Recommended Posts