#include "time.h" #include using namespace std; /** * Times in this program are represented by three integers: H, M, & S, representing * the hours, minutes, and seconds, respecitvely. */ // Create time objects Time::Time() // midnight { secondsSinceMidnight = 0; } Time::Time (int h, int m, int s) { secondsSinceMidnight = s + 60 * m + 3600*h; } // Access to attributes int Time::getHours() const { return secondsSinceMidnight / 3600; } int Time::getMinutes() const { return (secondsSinceMidnight % 3600) / 60; } int Time::getSeconds() const { return secondsSinceMidnight % 60; } // Calculations with time void Time::add (Time delta) { secondsSinceMidnight += delta.secondsSinceMidnight; } Time Time::difference (Time fromTime) { Time diff; diff.secondsSinceMidnight = secondsSinceMidnight - fromTime.secondsSinceMidnight; } /** * Read a time (in the format HH:MM:SS) after skipping any * prior whitepace. */ void Time::read (std::istream& in) { char c; int hours, minutes, seconds; in >> hours >> c >> minutes >> c >> seconds; Time t (hours, minutes, seconds); secondsSinceMidnight = t.secondsSinceMidnight; } /** * Print a time in the format HH:MM:SS (two digits each) */ void Time::print (std::ostream& out) const { out << setfill('0') << setw(2) << getHours() << ':' << setfill('0') << setw(2) << getMinutes() << ':' << setfill('0') << setw(2) << getSeconds(); } // Comparison operators bool Time::operator< (const Time& time2) const { return secondsSinceMidnight < time2.secondsSinceMidnight; } bool Time::operator==(const Time& time2) const { return secondsSinceMidnight == time2.secondsSinceMidnight; }