mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-07-07 20:06:39 +02:00
Daterange: Added ::intersect
This commit is contained in:
parent
5409b25f5f
commit
3e404b7f57
2 changed files with 46 additions and 1 deletions
|
@ -71,7 +71,7 @@ bool Daterange::isEnded () const
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// Detect the following cases:
|
// Detect the following overlap cases:
|
||||||
//
|
//
|
||||||
// this |--------|
|
// this |--------|
|
||||||
// other |--------| false [1]
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
|
@ -44,6 +44,7 @@ public:
|
||||||
bool isStarted () const;
|
bool isStarted () const;
|
||||||
bool isEnded () const;
|
bool isEnded () const;
|
||||||
bool overlap (const Daterange&) const;
|
bool overlap (const Daterange&) const;
|
||||||
|
Daterange intersect (const Daterange&) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Datetime _start {0};
|
Datetime _start {0};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue