Reputation: 778
I don't know if am am loosing my mind or if this is even possible.
By the way, I am not using any standard libraries so this is not language dependent, hence why it's here and not on stackoverflow.
I have 2 time periods that are being passed to me in a int format.
1st period-
Start Time:
End Time:
2nd period-
Start Time:
End Time:
The Int's are minutes since midnight, so 7am would be 420 minutes.
So an example of a function I am trying to write is:
bool SpanMoreThan24Hours(int firstStartTime, int firstEndTime,
int secondStartTime, int secondEndTime)
{
....
}
I am not even sure this can be done.
P.S. Spanning midnight is fine, but it doesn't have to. Periods can't overlap.
So it could look like this:
420, 1140, 1260, 419
7am, 7pm, 9pm, 6:59am
- Valid
This would be valid since it doesn't span over 24 hours.
420, 1140, 1260, 421
7am, 7pm, 9pm, 7:01am
- Not valid
420, 60, 120, 419
7am, 1am,2am, 6:59am
- Valid
I have to make sure from 1st period start time till 2nd period end time is not over 24 hours.
Upvotes: 3
Views: 261
Reputation: 3902
If a period can start on monday and end on wednesday, there is no solution to the problem; otherwise, here is some C# to you:
int MinutesBetweenTwoTimes(int time1, int time2)
{
return time1 < time2 ? time2 - time1 : (1440-time1) + time2;
}
bool SpanMoreThan24Hours(int firstStartTime, int firstEndTime,
int secondStartTime, int secondEndTime)
{
int totalSpannedTime = MinutesBetweenTwoTimes(firstStartTime, firstEndTime)
+ MinutesBetweenTwoTimes(firstEndTime, secondStartTime)
+ MinutesBetweenTwoTimes(secondStartTime, secondEndTime);
return totalSpannedTime > 1440;
}
Upvotes: 6
Reputation: 7266
If I read it right, I think this works, no?
bool SpanMoreThan24Hours(int firstStartTime, int firstEndTime,
int secondStartTime, int secondEndTime)
{
// Logically, you'll only have a span > 24 hours if one of them spans midnight
if ( firstStartTime > firstEndTime || secondStartTime > secondEndTime )
{
// Check for period overlap:
// If the second period doesn't span midnight, this means the first does.
// So check for overlap:
if ( secondStartTime <= secondEndTime && firstEndTime > secondStartTime )
throw new Exception ("Periods cannot overlap");
// If *both* periods span midnight, this means they logically overlap.
else if ( firstStartTime > firstEndTime && secondStartTime > secondEndTime )
throw new Exception ("Periods cannot overlap");
// If we get to here, we know that the periods don't overlap, yet they span midnight
// The answer now is simply if the end of the second span is after the first begins
return secondEndTime > firstStartTime;
}
// Neither period span midnight, so overlap check is a snap:
if ( firstEndTime > secondStartTime )
throw new Exception ("Periods cannot overlap");
// No errors (no overlaps) and no midnight span means
// the only logical result is that this is not > 24 hours
return false;
}
Upvotes: 0
Reputation: 213
I may be misunderstanding, but couldn't you just check whether firstStartTime <= secondEndTime?
Upvotes: 0