integer to time

M

Marcus

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
 
V

Victor Bazarov

Marcus said:
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.

So, your number is what, a number of seconds and you need to extract
the hours, the minutes, and the seconds from it? If I tell you that I
have 1234 cents, how do you determine how many dollars I have? Let's
make it a bit more curious. If I tell you my height is 65 inches, what
is my height in feet/inches? Try doing the same thing with your number.

Hint: use / and % operators

V
 
R

rossum

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

As you say 60*60 gives hours, I assume that your original number,
62446, is to be treated as that many seconds.

Rather than start with the hours, it is probably better to start with
the minutes. Dividing by 60 will give the number of minutes, with
some seconds left over. In your example 62446 seconds gives 1040
minutes with 46 seconds left over: (60 * 1040) + 46 = 62446.

Now take the 1040 minutes and divide by 60 to see how many hours there
are. This gives 17 hours with 20 minutes left over: (60 * 17) + 20 =
1040.

So 62446 seconds = 17 hours, 20 minutes and 46 seconds or 17:20:46.

HTH

rossum


The ultimate truth is that there is no ultimate truth
 
M

Marcus

Thanks Rossum and Victor. Ok, the math part I now get. I was trying to
make it more complicated than it actually was... I was imagining some
exotic formating, 67000 seconds makes a lot more sense. :)

I can't figure out if there's a a way to write that as a single
expression, or if it's necessary to break it up into two parts? More
specifically, I'm not sure how to pass the remainder in c++... Victor
hinted at the % operator, which looks like it divides for the
remainder, but I'm not sure how to use it. Trying to find some tuts on
this right now.

Anyways, thanks for the help.

Regards, Marcus
 
C

codigo

Marcus said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top