Sprenger000 has asked for the wisdom of the Perl Monks concerning the following question:
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:
Some assumptions about the data that will always be true:@array = ( [ '03:30', '05:45' ], [ '05:45', '09:15' ], [ '10:00', '11:35' ], [ '11:00', '15:40' ] );
(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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Determining gaps in time ranges
by kvale (Monsignor) on Apr 02, 2004 at 01:50 UTC | |
|
Re: Determining gaps in time ranges
by BrowserUk (Patriarch) on Apr 02, 2004 at 01:44 UTC | |
by tachyon (Chancellor) on Apr 02, 2004 at 02:41 UTC | |
by Sprenger000 (Initiate) on Apr 02, 2004 at 03:15 UTC | |
by BrowserUk (Patriarch) on Apr 02, 2004 at 03:32 UTC | |
by tachyon (Chancellor) on Apr 02, 2004 at 07:54 UTC | |
by tachyon (Chancellor) on Apr 02, 2004 at 07:45 UTC | |
|
Re: Determining gaps in time ranges
by PodMaster (Abbot) on Apr 02, 2004 at 02:34 UTC | |
|
Re: Determining gaps in time ranges
by davido (Cardinal) on Apr 02, 2004 at 01:36 UTC | |
|
Re: Determining gaps in time ranges
by fglock (Vicar) on Apr 02, 2004 at 21:10 UTC |