"Marcus" <mc*******@walla.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
I was wondering if someone could help explain how an integer could be
transformed into a time format. For instance I have an integer 62446
which should read 17:xx:xx or hhmmss (The colon seperators are not
necessary, as I don't mind it staying in an integer format... but I'd
just like to be able to recognize the time when printing). I know to
divide the entire number by 60*60 to get the hour, but I'm unsure how
to derive the rest of the numbers. If anyone could give me an example
of a formula for this and/or explain how the integer is derived from
the time format, it would really go a long way in helping me
understand.
Any help is greatly appreicated.
Regards,
Marcus
Should be simple arithmetic.
24 hours * 60 min * 60 sec = 86,400 total seconds per day
so... integer is in range
from 0 to 86,399 is 86,400 values
------
#include <iostream>
using std::cout;
const int hour = 3600;
const int min = 60;
class Time
{
int m_t; // member seconds
public:
Time(int t) : m_t(t)
{
}
~Time()
{
}
void getTime() const
{
int n_input = m_t;
int n_hour_result = n_input / hour; // hours
n_input -= n_hour_result * hour;
int n_min_result = n_input / min; // minutes residue
n_input -= n_min_result * min; // seconds residue
cout << n_hour_result;
cout << "::";
cout << n_min_result;
cout << "::";
cout << n_input;
cout << "\n";
} // getTime()
}; // Time
int main()
{
Time t(86399); // 23:59:59
t.getTime();
Time tt(3661); // 1:1:1 am
tt.getTime();
return 0;
}
-----------
Fix the cout formatting for single digits and you might
want to throw an exception if 0 > m_t > 86399.