Forum Academy Marketplace Showcase Pricing Features

How to count unique minutes in selected time-ranges

Does anyone have a good suggestion on how to achieve the following, without reinventing the wheel too much?

I have a Data Type “timerange” that contains a “from” and “until” timestamp. The data entries for this can be overlapping, i.e one time range can be from 4pm to 10pm, and another can be 8pm to 11pm on the same date.

In my app I will need the user to select multiple applicable timeranges. I then need to count the unique/non-overlapping minutes in the selection. So in the example above I only want to count the 8pm to 10pm minutes once. The won’t always overlap, so I can’t just extract the start of one and the end of another.

Search for the range that have the earliest start. Extract this date.
Search for the range that have the bigger end. Extract this date.
Do one minus other. Extract the minutes.

Thanks, unfortunately that solution won’t be adequate. They won’t always overlap, it could for example be 4pm to 10pm on two consequitive days. Further it may be many such entries in a single user selection, some overlapping and some don’t.