lundi 17 août 2015

How to correct the date conflict checking ruby

I want to test if two date intervals overlap. That's what I've come up with:

def conflict?(booking, testdate)
  !(booking.last <= testdate.first || booking.first >= testdate.last)
end

irb> conflict?(["2015-02-16", "2015-02-18"],["2015-02-17", "2015-02-21"])
=> true
irb> conflict?(["2015-02-16", "2015-02-18"],["2015-02-19", "2015-02-21"])
=> false

irb(main):011:0> conflict?(["2015-02-16", "2015-02-18"],["2015-02-18", "2015-02-21"])
=> false

This actually should've returned true

irb> conflict?(["2015-02-16", "2015-02-18"],["2015-02-13", "2015-02-16"])
=> false 

irb> conflict?(["2015-02-16", "2015-02-18"],["2015-02-18", "2015-02-21"])
=> false

Aucun commentaire:

Enregistrer un commentaire