Daterange: Added ::intersect

This commit is contained in:
Paul Beckingham 2016-04-16 09:52:12 -04:00
parent 5409b25f5f
commit 3e404b7f57
2 changed files with 46 additions and 1 deletions

View file

@ -71,7 +71,7 @@ bool Daterange::isEnded () const
}
////////////////////////////////////////////////////////////////////////////////
// Detect the following cases:
// Detect the following overlap cases:
//
// this |--------|
// other |--------| false [1]
@ -111,3 +111,47 @@ bool Daterange::overlap (const Daterange& other) const
}
////////////////////////////////////////////////////////////////////////////////
// Calculate the following intersection cases:
//
// this |--------|
// other |--------|
// other |----|
// other |--------|
// other |-------------|
// other |...
// other |...
//
// this |...
// other |--------|
// other |----|
// other |...
// other |...
//
Daterange Daterange::intersect (const Daterange& other) const
{
Daterange result;
if (overlap (other))
{
// Intersection is choosing the later of the two starts, and the earlier of
// the two ends, of two overlapping ranges.
result.start (start () < other.start () ? start () : other.start ());
if (isEnded ())
{
if (other.isEnded ())
result.end (end () < other.end () ? end () : other.end ());
else
result.end (end ());
}
else
{
if (other.isEnded ())
result.end (other.end ());
}
}
return result;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -44,6 +44,7 @@ public:
bool isStarted () const;
bool isEnded () const;
bool overlap (const Daterange&) const;
Daterange intersect (const Daterange&) const;
private:
Datetime _start {0};