/*********************************************************************
The time class stores times as hours, minutes, seconds and hundredths
of a second. It is accurate to 1/100 of a second and has a maximum
storage of MAX_INT hours.
John Dolan September 2000
// author John Dolan: slightly edited by Travis Dillon for output
**********************************************************************/
#include "time.h"
using namespace std;
// THE CONSTRUCTORS
Time::Time(){
hours = minutes = seconds = 0;
}
Time::Time(int init_sec){
back_to(init_sec);
}
Time::Time(int hrs,int min,int sec){
hours = hrs;
minutes = min;
seconds = sec;
}
// MEMBER FUNCTIONS
// Set_time sets the time of the object to whatever parameters are passed
void Time::set_time(int hrs,int min,int sec){
hours = hrs;
minutes = min;
seconds = sec;
}
// This overloaded set_time works with a gross number of seconds and sets
// time based on those seconds
void Time::set_time(int n_sec){
back_to(n_sec);
}
// all() is a private member function that conversts time into a single
// long integer to be used for comparisons and calculations
long Time::all() const{
return long(hours*3600+minutes*60+seconds);
}
// back_to(long) converts a long int back into the time format
void Time::back_to(long all_num){
hours = all_num/3600;
all_num %= 3600;
minutes = all_num/60;
all_num %= 60;
seconds = all_num;
}
// I/O OPERATORS
istream& operator >>(istream& ins,Time& t){
while(!isdigit(ins.peek()))
ins.ignore(); // ignore leading spaces
ins>>t.hours;
if(ins.peek() != ':'){
cout<<"Error in time format.\n";
return ins;
}
ins.ignore(); //eat the :
ins>>t.minutes;
if(ins.peek() != ':'){ // time being entered has only hours and
t.seconds=0; // minutes
return ins;
}
else{
ins.ignore(); // eat the :
ins>>t.seconds;
}
return ins;
}
// Insertion Operator for time
ostream& operator <<(ostream& outs, Time& t){
if(t.hours>0){
if(t.hours < 10) outs << '0';
outs<<t.hours;
outs<<':';
}
else
{
outs << "00:";
}
if(t.minutes<10)
outs<<'0';
outs<<t.minutes;
outs<<':';
if(t.seconds<10)
outs<<'0';
outs<<t.seconds;
return outs;
}
// ARITHEMETIC OPERATORS
Time operator +(const Time& t1, const Time& t2){
Time tmp;
tmp.back_to(t1.all() + t2.all());
return tmp;
}
Time operator -(const Time& t1, const Time& t2){
Time tmp;
tmp.back_to(t1.all() - t2.all());
return tmp;
}
Time operator /(const Time& t1, const int& num){
Time tmp;
if(num > 0)
tmp.back_to(t1.all()/num);
return tmp;
}
// COMPARISON OPERATORS
bool operator ==(const Time& t1, const Time& t2){
return (t1.all() == t2.all());}
bool operator !=(const Time& t1, const Time& t2){
return (t1.all() != t2.all());}
bool operator >=(const Time& t1, const Time& t2){
return (t1.all() >= t2.all());}
bool operator <=(const Time& t1, const Time& t2){
return (t1.all() <= t2.all());}
bool operator >(const Time& t1, const Time& t2){
return (t1.all() > t2.all());}
bool operator <(const Time& t1, const Time& t2){
return (t1.all() < t2.all());}