overloaded operator+ and converting time


What I am trying to do is add seconds to my object and return my object in a hh:mm:ss format. I am not sure how to convert the total seconds into the proper format.


Time24 Time24::operator+(const int second) const
{
int secResult;
int minResult;
Time24 temp;
temp.sec = sec + second;
if ( temp.sec > 59)
{
secResult = temp.sec / 60;

temp.min = min + secResult;

}
temp.min = min;
temp.hr = hr;

return temp;
I did not test but hope the code below is correct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const Time24 Time24::operator +( int second ) const 
{
   Time24 temp;

   seconds += temp.sec;
   temp.sec = seconds % 60;

   seconds /= 60;

   seconds += temp.min;
   temp.min = seconds % 60;

   seconds /= 60;

   seconds += temp.hr;
   temp.hr = seconds % 24;
 
   return temp; 
}
Topic archived. No new replies allowed.