473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

integer to time

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 2625
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
On 11 Apr 2005 14:27:53 -0700, "Marcus" <mc*******@walla.com> 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.

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
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

"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.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: kpp9c | last post by:
I am kind of in a bit of a jam (okay a big jam) and i was hoping that someone here could give me a quick hand. I had a few pages of time calculations to do. So, i just started in on them typing...
17
by: newbiecpp | last post by:
I have hard time to understand run-time environment. Let assume that I have a program that has a simple variable alpha. When this variable is statically allocated, the compiler can use the...
1
by: heirou | last post by:
I'm a novice in this subject....I've made a database that requires a time conversion. For example, if local time is 1200, determine the time in Korea. I use two fields: a date field, and a time...
3
by: Chandu | last post by:
Hi, I am working on awk programming which is similar to C programming and have got a doubt about time function returning a float value. Ex: 01:01:30 should return 61.5 when i have tried my way i...
0
by: Ed Bick | last post by:
Well, I read today about how I could set the dropdown value to False to change the control from presenting a Calendar to cycling through the times. Before getting that, I developed a custom Time...
3
by: cj | last post by:
If I want to check to see if it's after "11:36 pm" what would I write? I'm sure it's easy but I'm getting tired of having to work with dates and times. Sometimes I just want time or date. And...
4
by: CDMAPoster | last post by:
In: http://groups.google.com/group/comp.databases.ms-access/msg/f06bd4c45037ef29 Lyle wrote: :Terry Kreft wrote: : :>Suck it and See? :
11
Zwoker
by: Zwoker | last post by:
Hi All, I'm using MS Access 2003. I have been trying to find out why portions of my VBA code have been running slower than expected, using code like the following: Dim StartTime, EndTime As...
1
by: hackerbob | last post by:
I'm trying to create a constant time event timer. Basically, a routine can set a callback to be called n ms from the current time, and the main event loop will wait until the delta between the...
1
by: ndedhia1 | last post by:
I was reading in a log file like this that had no milliseconds: QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40 CST 2009 170.137.15.155 Class key = 601650761 block size QuoteBlockTiming...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.