// // Time // #include #include #include "Time.h" // pre - output stream, other time // post - print other time ostream & operator<<(ostream & out, const Time & other) {out << other.hours << ":" ; if(other.minutes < 10) out << "0" ; out << other.minutes << " " << other.ampm ; return out ; } // pre - none // post - set to 12:01 am Time:: Time() {hours = 12 ; minutes = 01 ; ampm = "AM" ;} // pre - none // post - set valid hours, minutes, am/pm Time::Time(int hours, int minutes, string ampm) {if(hours < 1 || hours > 12) hours = 1 ; this->hours = hours ; if(minutes < 0 || minutes > 59) minutes = 0 ; this->minutes = minutes ; if(ampm != "AM" && ampm != "PM") ampm = "AM" ; this->ampm = ampm ;} // pre - other time exists // post - set this time to other Time::Time(const Time & other) {if(other.hours < 1 || other.hours > 12) this->hours = 1 ; else this->hours = other.hours ; if(other.minutes < 0 || other.minutes > 59) this->minutes = 0 ; else this->minutes = other.minutes ; if(other.ampm != "AM" && other.ampm != "PM") this->ampm = "AM" ; else this->ampm = other.ampm ;} // pre - time in string format // post - set this time to string time Time::Time(string tmp){ // must be in format H:MM XM or HH:MM XM if(tmp[1] == ':'){ this->hours = atoi(tmp.substr(0, 1).c_str()) ; this->minutes = atoi(tmp.substr(2,2).c_str()) ; this->ampm = tmp.substr(5,2) ;} else if(tmp[2] == ':'){ this->hours = atoi(tmp.substr(0, 2).c_str()) ; this->minutes = atoi(tmp.substr(3,2).c_str()) ; this->ampm = tmp.substr(6,2) ;} else{ // poor format this->hours = 12 ; this->minutes = 1 ; this->ampm = "AM" ;} } // pre - other time exists // post - compare this to other bool Time::operator==(const Time & other) const {return this->hours == other.hours && this->minutes == other.minutes && this->ampm == other.ampm ;} // pre - other time exists // post - compare this to other bool Time::operator!=(const Time & other) const {return !(this->hours == other.hours && this->minutes == other.minutes && this->ampm == other.ampm ) ;} // pre - other time exists // post - compare this to other bool Time::operator<(const Time & other) const {if(this->ampm == other.ampm) if(this->hours == other.hours) return this->minutes < other.minutes ; else return this->hours < other.hours ; else return this->ampm == "AM" ; } // pre - other time exists // post - compare this to other bool Time::operator<=(const Time & other) const {if(this->ampm == other.ampm) if(this->hours == other.hours) return this->minutes <= other.minutes ; else return this->hours < other.hours ; else return this->ampm == "AM" ; } // pre - other time exists // post - compare this to other bool Time::operator>(const Time & other) const {if(this->ampm == other.ampm) if(this->hours == other.hours) return this->minutes > other.minutes ; else return this->hours > other.hours ; else return this->ampm == "PM" ; } // pre - other time exists // post - compare this to other bool Time::operator>=(const Time & other) const {if(this->ampm == other.ampm) if(this->hours == other.hours) return this->minutes >= other.minutes ; else return this->hours > other.hours ; else return this->ampm == "PM" ; }