//******************************************************************* // // IMPLEMENTATION FILE (Time.cpp) // This file implements the Time member functions //****************************************************************** #include // required for system calls in default constructor #include #include "Time.h" //****************************************************************** void Time::Set( /* in */ int hours, /* in */ int minutes, /* in */ int seconds ) // Precondition: // 0 <= hours <= 23 && 0 <= minutes <= 59 && 0 <= seconds <= 59 // Postcondition: // hrs == hours && mins == minutes && secs == seconds { hrs = hours; mins = minutes; secs = seconds; } //****************************************************************** void Time::Increment() // Postcondition: // Time has been advanced by one second, with // 23:59:59 wrapping around to 0:0:0 { secs++; if (secs > 59) { secs = 0; mins++; if (mins > 59) { mins = 0; hrs++; if (hrs > 23) hrs = 0; } } } //****************************************************************** void Time::Write() const // Postcondition: // Time has been output in the form HH:MM:SS { if (hrs < 10) cout << '0'; cout << hrs << ':'; if (mins < 10) cout << '0'; cout << mins << ':'; if (secs < 10) cout << '0'; cout << secs; } //****************************************************************** bool Time::Equal( /* in */ Time otherTime ) const // Postcondition: // Function value == true, if this time equals otherTime // == false, otherwise { return (hrs == otherTime.hrs && mins == otherTime.mins && secs == otherTime.secs); } //****************************************************************** bool Time::LessThan( /* in */ Time otherTime ) const // Precondition: // This time and otherTime represent times in the same day // Postcondition: // Function value == true, if this time is earlier // in the day than otherTime // == false, otherwise { return (hrs < otherTime.hrs || hrs == otherTime.hrs && mins < otherTime.mins || hrs == otherTime.hrs && mins == otherTime.mins && secs < otherTime.secs); } //****************************************************************** int Time::Hours() const // Postcondition: // Return value is hrs { return hrs; } //****************************************************************** int Time::Minutes() const // Postcondition: // Return value is mins { return mins; } //****************************************************************** int Time::Seconds() const // Postcondition: // Return value is seconds { return secs; } //****************************************************************** Time::Time() // Defualt constructor // Postcondition: // Time is set to current time { time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); hrs = timeinfo->tm_hour; mins = timeinfo->tm_min; secs = timeinfo->tm_sec; } //****************************************************************** Time::Time( /* in */ int hours, /* in */ int minutes, /* in */ int seconds ) // Precondition: // 0 <= hours <= 23 && 0 <= minutes <= 59 // && 0 <= seconds <= 59 // Postcondition: // hrs == hours && mins == minutes && secs == seconds { hrs = hours; mins = minutes; secs = seconds; }