Check overlap in hourly intervals

0

Currently I have the need to compare if a section in hours overlaps with another stretch of hours, the truth is that I'm getting a mess and I can not see what is not right, I attach what I have done to see if you see the error:

  • The stretches are HoraIni-HoraFin and stretchFrom-stretchTo

  • are whole numbers

  • a stretch for example from 1 to 3 and the other from 3 to 6 are not considered overlapping

  • If ((tramoDesde < HoraIni And HoraIni > tramoHasta) Or (tramoDesde < HoraFin And tramoHasta > HoraFin) Or (tramoDesde >= HoraIni And tramoHasta <= HoraFin)) Then
        Solapados = True
    End If
    

    I imagine that in the end it will be a detail or something in the comparison

        
    asked by U. Busto 06.04.2017 в 16:33
    source

    2 answers

    0

    There is a library in CodeProject that is in charge of reviewing this and other things

    link

    I have not used it but it has a good reference

    link

        
    answered by 06.04.2017 в 16:39
    0

    There is a very simple formula to check if two periods overlap:

    int HoraIni = 3;
    int HoraFin = 6;
    int tramoDesde = 1;
    int tramoHasta = 8;
    bool solape = HoraIni < tramoHasta && tramoDesde < HoraFin;
    

    Edit

    Sorry, it was VB.NET:

    Dim HoraIni As Integer = 3
    Dim HoraFin As Integer = 6
    Dim tramoDesde As Integer = 1
    Dim tramoHasta As Integer = 8
    Dim solape As Boolean = HoraIni < tramoHasta AndAlso tramoDesde < HoraFin
    
        
    answered by 06.04.2017 в 16:43