Given your starting point the solution is pretty straight forward. Assume that you start with all times being available so your initial display will be:
Jan 1st 12:00am - Dec 31st 11:59pm
Assuming that the appointment times are in chronological order and there is no overlap with any of them (if not, you should be able to make an algorithm to fix up the list for you) the solution is simple.
For each "unavailable" slot:
- Take the last available slot and split it into two
- set the start time of the first slot to the start time of the original
- set the end time of the first slot to the start time of the "unavailable" slot
- set the start time of the second slot to the end time of the "unavailable" slot
- set the end time of the second slot to the end time of the original.
Once you've down that you'll have a list of available slots. Here's an example:
Original availability is Jan 1st 12:00 AM - Dec 31st 11:59 PM
you have the following "unavailable" slots
Jan 15th 12:00 AM - Jan 15th 11:59 PM
Jan 20th 12:00 PM - Feb 4th 9:00 AM
Feb 14th 1:00 PM - Feb 14th 5:00 PM
The first run of the algorithm would look like so
Original Jan 1st 12:00 AM - Dec 31st 11:59 PM
Split into two
First runs Start of original to start of "unavailable"
Jan 1st 12:00 AM - Jan 15th 12:00 AM
Second runs End of "unavailable" to end of original
Jan 15th 11:59 PM - Dec 31st 11:59 PM
So now you have two available slots
Jan 1st 12:00 AM - Jan 15th 12:00 AM
Jan 15th 11:59 PM - Dec 31st 11:59 PM
Do the same operations with the next two and you're home free.
Don't forget edge cases.
What happens when you have an available slot that is essential zero in length?
What happens when one of the "unavailable slots runs past the end or before the beginning of your window?