This is the one thing I could never get to work.
My problem is to detect the end of one day and the start of the next and then splitting the diff into each day.
Imagine you want to calculate a pay rate but it has to span across midnight.
It also applies to calculating time to run on timed system, or time diff it should’ve run.
This is obviously a trivial problem in systems where the starting and ending times are data structures that contain both date and time. But there are plenty of systems that don’t do this. It’s very common for timekeeping systems to have a single record that contains a date, a start time, and an end time. In this case, the problem is less trivial.
There are, I think, two questions you’re asking:
The first question is easily answered: if the end time is before the start time, the time span has crossed midnight.
If this is the case, then you have two amounts to calculate. The amount of the time span that occurred on the date is the time between the start time and midnight. The amount of the time span that occurred on the next date is the time between midnight and the end time (i.e. just the end time).