Elasped Time - in hr, min and seconds

N

Neo

Is there any function in std. C to convert seconds into hour, min and sec. ?
Lets say the following code takes 129 seconds, how do i get components of
time in "hour min and second" format? Is there any std. library function to
do this?
it should give "0 hrs 2 min 9 sec"


/* The following code only prints elapsed time in seconds */
#include<stdio.h>
#include<limits.h>
#include<time.h>

int main(void)
{
time_t t1, t2;
unsigned int i,j, k;

t1 = time(NULL);

/* some code to timed here.... */
/* ..... */
for(i = 0; i < ~0; i++);
/* ... end of processing ... */

t2 = time(NULL);
k = difftime(t2, t1);
printf("Time Elapsed = %d seconds\n", k);

return 0;
}


-Neo
 
G

Gregory Toomey

Neo said:
Is there any function in std. C to convert seconds into hour, min and sec.
? Lets say the following code takes 129 seconds, how do i get components
of time in "hour min and second" format? Is there any std. library
function to do this?
it should give "0 hrs 2 min 9 sec"
% is the infix modulus operator.
If you cant now get the answer, give up programming.

gtoomey
 
C

CBFalconer

Neo said:
Is there any function in std. C to convert seconds into hour, min
and sec. ? Lets say the following code takes 129 seconds, how do i
get components of time in "hour min and second" format? Is there
any std. library function to do this?
it should give "0 hrs 2 min 9 sec"

/* The following code only prints elapsed time in seconds */
#include<stdio.h>
#include<limits.h>
#include<time.h>

int main(void)
{
time_t t1, t2;
unsigned int i,j, k;

t1 = time(NULL);

/* some code to timed here.... */

t2 = time(NULL);
k = difftime(t2, t1);
printf("Time Elapsed = %d seconds\n", k);
return 0;
}

try (untested):

#include <stdlib.h>
#include <time.h>

long int hrs, mins, secs;
ldiv_t result;
time_t t1, t2;

.....
t1 = time(NULL);
....
t2 = time(NULL);
secs = difftime(t2, t1);
result = ldiv(secs, 60);
secs = result.rem;
mins = result.quot;
mins = ldiv(mins, 60);
mins = result.rem;
hrs = result.quot;
 
S

Steven K. Mariner

Is there any function in std. C to convert seconds into hour,
> min and sec. ?

No, but it sounds like a perfectly wonderful routine to add to your
personal library of routines.

Then you'd have the added advantage of learning how to maintain an
object library in C.

Or you could take the cheap way out and make it a standalone source
file and use a makefile to include it for the program. Assuming
'make' is supported on your platform, of course.

_________________
Steven K. Mariner
(e-mail address removed)
http://home.earthlink.net/~marinersk/
http://www.whirlyjigmusic.com/
 
S

Steven K. Mariner

"If you want to post a followup via groups.google.com, don't
> use the broken "Reply" link at the bottom of the article.
> Click on "show options" at the top of the article, then click
> on the "Reply" at the bottom of the article headers." - Keith
> Thompson

Thanks for the tip.

I was wondering why the formatting was sometimes quite stupid. Hadn't
looked into it enough to hit critical mass for intuitive resolution.
Figured it was just another one of those things you live with in an
imperfect Internet world.

_________________
Steven K. Mariner
(e-mail address removed)
http://home.earthlink.net/~marinersk/
http://www.whirlyjigmusic.com/
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top