mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-07-07 20:06:36 +02:00
Duration
- Implemented operator+=, operator-=. - Added unit tests.
This commit is contained in:
parent
b70f4e8528
commit
8ff3a1675d
3 changed files with 89 additions and 5 deletions
|
@ -155,9 +155,60 @@ Duration& Duration::operator= (const Duration& other)
|
|||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
Duration& Duration::operator- (const Duration& other)
|
||||
Duration Duration::operator- (const Duration& other)
|
||||
{
|
||||
throw std::string ("Error: Duration::operator- unimplemented");
|
||||
int left = mSecs * ( mNegative ? -1 : 1);
|
||||
int right = other.mSecs * (other.mNegative ? -1 : 1);
|
||||
|
||||
left -= right;
|
||||
|
||||
Duration result;
|
||||
result.mSecs = abs (left);
|
||||
result.mNegative = left < 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
Duration Duration::operator+ (const Duration& other)
|
||||
{
|
||||
int left = mSecs * ( mNegative ? -1 : 1);
|
||||
int right = other.mSecs * (other.mNegative ? -1 : 1);
|
||||
|
||||
left += right;
|
||||
|
||||
Duration result;
|
||||
result.mSecs = abs (left);
|
||||
result.mNegative = left < 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
Duration& Duration::operator-= (const Duration& other)
|
||||
{
|
||||
int left = mSecs * ( mNegative ? -1 : 1);
|
||||
int right = other.mSecs * (other.mNegative ? -1 : 1);
|
||||
|
||||
left -= right;
|
||||
|
||||
mSecs = abs (left);
|
||||
mNegative = left < 0;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
Duration& Duration::operator+= (const Duration& other)
|
||||
{
|
||||
int left = mSecs * ( mNegative ? -1 : 1);
|
||||
int right = other.mSecs * (other.mNegative ? -1 : 1);
|
||||
|
||||
left += right;
|
||||
|
||||
mSecs = abs (left);
|
||||
mNegative = left < 0;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
|
|
@ -43,7 +43,10 @@ public:
|
|||
bool operator> (const Duration&);
|
||||
bool operator>= (const Duration&);
|
||||
Duration& operator= (const Duration&);
|
||||
Duration& operator- (const Duration&);
|
||||
Duration operator- (const Duration&);
|
||||
Duration operator+ (const Duration&);
|
||||
Duration& operator-= (const Duration&);
|
||||
Duration& operator+= (const Duration&);
|
||||
~Duration (); // Destructor
|
||||
|
||||
operator time_t () const;
|
||||
|
@ -56,7 +59,7 @@ public:
|
|||
static bool valid (const std::string&);
|
||||
void parse (const std::string&);
|
||||
|
||||
private:
|
||||
protected:
|
||||
time_t mSecs;
|
||||
bool mNegative;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue