This will probably seem like a Comp Sci exam question, but honestly it's a real problem I need to solve and it's driving me nuts.

I have a list of time ranges: pairs of numbers representing a starting time and an ending time. The list consists of an arbitrary number of elements the number of which is not controllable within the scope of the application. These numbers are in the general time format of HH:MM on a 24 hour scale.

I would like to determine what (if any) ranges of time are not included in any of the ranges denoted by the pairs in my list (within 0-24 hours).

As always, some code may help to visualize. For lack of anything more inspired we'll put the source data in an array structure:

@array = ( [ '03:30', '05:45' ], [ '05:45', '09:15' ], [ '10:00', '11:35' ], [ '11:00', '15:40' ] );
Some assumptions about the data that will always be true:

(I feel that last one needs explaining: while 24:00 is not a real time, it's needed to indicate a "full" ending range. If a user wants to specify "11:00pm until Midnight" I can't use 00:00 since that's technically last midnight, and I don't want to force the user into specifying something like 23:59 since that leaves a small gap. So 24 is a way to indicate the full range of time up until midnight, whether that's 23:59 or 23:59.99999.)

The specific results I need from these are the ranges of time not included in any of the pairs in the list. In the example code from above, it would be nice to see a result set that looks like:

( [ '00:00', '03:30' ], [ '09:15', '10:00' ], [ '15:40', '24:00' ] );

Note that having two ranges back to back does NOT result in a "gap": the 3:30-5:45 and 5:45-9:15 ranges should be considered contiguous.

I just have no idea how to get this result from the previous sample data. I'd really like to avoid hacks like testing each minute in a 24 hour period, as that would probably not be performance-effective...

Any suggestions would be very much appreciated! I'm also open to changes on how the data is structured and formatted, if changes will help with possible solutions. Thanks!


In reply to Determining gaps in time ranges by Sprenger000

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.