jueves, 21 de febrero de 2013

howto detect date collisions or conflicts

Today during a test of a client's app I found an interesting problem, how to detect date collisions or conflicts?
Suppose you have to create an agenda app, that allows it's users to create meetings or appointments and it must not allow two events to happen at the same time, for simplicity let's just say "all events created must not be in schedule conflict with other events"
It seems simple to resolve at first glance, but allow me to show you some examples of why it is not: Suppose you have two events A which last 60 minutes and B which last 90 minutes.
There are so many ways that we can create this two events in our agenda so that there will be a schedule conflict (date collision)
  1. Setting A to start and same time as B http://screencloud.net/v/ChX3
  2. Setting A to start a few minutes before B http://screencloud.net/v/73vI
  3. Setting B to start a few minutes before A http://screencloud.net/v/uyaG
  4. Setting A to start a few minutes later of B http://screencloud.net/v/lpLg
  5. ...

If you try to list all possible combinations in which A and B are in conflict your code may become pretty big and very difficult to maintain for something as simple as "detect date collisions", luckily there is another way more simple: Instead of search all possible conditions in which A and B are in conflict lets search for the possibles situations in which they are not
  1. A starts and ends before B http://screencloud.net/v/hEXt
  2. B starts and ends before A http://screencloud.net/v/tR6X

We can use this conditions and some boolean logic to create for example a mysql query:
I hope this is useful for anyone of you.
Regards
Spanish version

No hay comentarios:

Publicar un comentario

Deja tu comentario :D