446,227 Members | 1,376 Online Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

# integer to time

 P: n/a 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 Jul 23 '05 #1
4 Replies

 P: n/a Marcus wrote: 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 Jul 23 '05 #2

 P: n/a On 11 Apr 2005 14:27:53 -0700, "Marcus" wrote: I was wondering if someone could help explain how an integer could betransformed into a time format. For instance I have an integer 62446which should read 17:xx:xx or hhmmss (The colon seperators are notnecessary, as I don't mind it staying in an integer format... but I'djust like to be able to recognize the time when printing). I know todivide the entire number by 60*60 to get the hour, but I'm unsure howto derive the rest of the numbers. If anyone could give me an exampleof a formula for this and/or explain how the integer is derived fromthe time format, it would really go a long way in helping meunderstand.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 Jul 23 '05 #3

 P: n/a 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 Jul 23 '05 #4

 P: n/a "Marcus" 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 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. Jul 23 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion. 