#include "Time.h" #include #include using namespace std; Time::Time(int h, int m, int s) { Set(h, m, s); } // ------------------------------------------------------------------------------------------------------------------------- // Relational operators. // ------------------------------------------------------------------------------------------------------------------------- bool Time::operator == (const Time & rhs) const { return this->seconds == rhs.seconds; } bool Time::operator != (const Time & rhs) const { return !(*this == rhs); } bool Time::operator > (const Time & rhs) const { return this->seconds > rhs.seconds; } bool Time::operator >= (const Time & rhs) const { return this->seconds >= rhs.seconds; } bool Time::operator < (const Time & rhs) const { return this->seconds < rhs.seconds; } bool Time::operator <= (const Time & rhs) const { return this->seconds <= rhs.seconds; } // ------------------------------------------------------------------------------------------------------------------------- // Mathematical operators. // ------------------------------------------------------------------------------------------------------------------------- Time Time::operator + (const Timespan & amount) const { Time result = *this; result.seconds += amount; return result; } Time Time::operator - (const Timespan & amount) const { Time result = *this; result.seconds -= amount; return result; } Timespan Time::operator - (const Time & rhs) const { return this->seconds - rhs.seconds; } Time& Time::operator += (const Timespan & amount) { this->seconds += amount; return *this; } Time& Time::operator -= (const Timespan & amount) { this->seconds -= amount; return *this; } Time& Time::operator ++ () { this->seconds++; return *this; } Time Time::operator ++ (int) { Time orig = *this; ++(*this); return orig; } Time& Time::operator -- () { this->seconds--; return *this; } Time Time::operator -- (int) { Time orig = *this; --(*this); return orig; } // ------------------------------------------------------------------------------------------------------------------------- // Additional operators. // ------------------------------------------------------------------------------------------------------------------------- ostream & operator << (ostream & os, const Time & t) { os << t.GetHour() << ":" << t.GetMinute() << ":" << t.GetSecond(); return os; } istream & operator >> (istream & is, Time & t) { int h, m, s; is >> h >> m >> s; t.Set(h, m, s); return is; } bool Time::operator ! () const { return seconds == 0; } // ------------------------------------------------------------------------------------------------------------------------- // Additional methods. // ------------------------------------------------------------------------------------------------------------------------- void Time::Set(int h, int m, int s) { seconds = h * 3600 + m * 60 + s; } int Time::GetHour() const { return seconds / 3600; } int Time::GetMinute() const { int secondsInFullHours = GetHour() * 3600; int additionalSeconds = seconds - secondsInFullHours; return additionalSeconds / 60; } int Time::GetSecond() const { return seconds % 60; } string Time::ToString() const { stringstream stm; stm << GetHour() << ":" << GetMinute() << ":" << GetSecond(); return stm.str(); }