How to know if several periods of time overlap?

3

I need to iterate an array of date objects, with duration and get how many overlaps each of them.

But I'm looking to do it efficiently and quickly. I'm looking for frameworks to manage time periods but I have not found anything.

I developed this, but it really does not work well.

function numeroSolapacionesSesiones(sesion, lista)
{
    var sesiones = new Array();

    if(lista != null && lista.length > 0)
    {
        var fecha = convierteCadenaFechaHoraIsoFechaLocal(sesion.fechaCelebracionStr);
        var hora = fecha.substring(11,16);  
        var paramsHora = hora.split(":");
        var duracion = 90;
        var horasDuracion = duracion/60;
        var minutosRestoDuracion = duracion%60;
        var minutosTotalesConDuracion = parseInt(paramsHora[1]) + minutosRestoDuracion;
        var horasSumables = minutosTotalesConDuracion/60;
        var minutosFinales = minutosTotalesConDuracion%60;

        var horaInicio = parseInt(paramsHora[0]);
        var horaFinal = parseInt(paramsHora[0]) + horasSumables;
        var minutoInicio = parseInt(paramsHora[1]);
        var minutoFinal = minutosFinales;


        for(var numSesion in lista)
        {           
            var sesionItem = lista[numSesion];
            var fechaItem = convierteCadenaFechaHoraIsoFechaLocal(sesionItem.fechaCelebracionStr);
            var horaItem = fechaItem.substring(11,16);  
            var paramsHoraItem = horaItem.split(":");
            var duracionItem = 90;

            var horasDuracionItem = duracionItem/60;
            var minutosRestoDuracionItem = duracionItem%60;
            var minutosTotalesConDuracionItem = parseInt(paramsHoraItem[1]) + minutosRestoDuracionItem;
            var horasSumablesItem = minutosTotalesConDuracionItem/60;
            var minutosFinalesItem = minutosTotalesConDuracionItem%60;

            var horaInicioItem = parseInt(paramsHoraItem[0]);
            var horaFinalItem = parseInt(paramsHoraItem[0]) + horasSumablesItem;
            var minutoInicioItem = parseInt(paramsHoraItem[1]);
            var minutoFinalItem = minutosFinalesItem;

            if(horaInicio == horaInicioItem || horaFinal == horaInicioItem || horaInicio == horaFinalItem)
            {

                if(!(horaFinal == horaInicioItem && minutoFinal < minutoInicioItem || horaInicio == horaFinalItem && minutoFinalItem < minutoInicio))               
                {                   
                    sesiones.push(sesionItem);                                  
                }
            }
        }
    }

    return sesiones;

}
    
asked by Carlos Lara 23.02.2016 в 15:12
source

2 answers

3

If you are looking for a library, the best one I've used is moment.js

There is an extension based on this to work with ranges

moment-range

You could do

var start  = new Date(2012, 4, 1);
var end    = new Date(2012, 4, 23);

var range  = moment.range(start, end);

var dateInRange    = new Date(2012, 4, 15);
range.contains(dateInRange); //return true

As you will see it is very simple to create ranges and depsues validate if this content

    
answered by 23.02.2016 / 16:15
source
1

See if at the time of doing the check you can convert the dates to timestamps, that will facilitate the calculation since the timestamps are simple numerical values. Timestamps are a way to measure time in seconds. Specifically, a timestamp is the number of seconds that elapsed since 01/01/1970. For example, the timestamp of 01/01/2016 at 00:00:00 is 1451606400

If you can pass them to timestamp, then you can use this super simple function to check if they overlap

function hayInterseccion(desde1, hasta1, desde2, hasta2) {
    if (desde2 <= desde1 && hasta2 >= hasta1) {
        return true;
    }

    if (desde2 >= desde1 && hasta2 <= hasta1) {
        return true;
    }

    if (desde2 <= desde1 && hasta2 <= hasta1 && hasta2 > desde1) {
        return true;
    }

    if (desde2 >= desde1 && hasta2 >= hasta1 && desde2 < hasta1) {
        return true;
    }

    return false;
}
    
answered by 23.02.2016 в 15:43