Elasped Time - in hr, min and seconds

  • Follow


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


0
Reply timeless_illusion (71) 3/28/2005 11:07:16 AM

Neo wrote:

> 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
0
Reply nospam258 (216) 3/28/2005 11:38:47 AM


Neo wrote:
> 
> 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;

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

0
Reply cbfalconer (19183) 3/28/2005 1:33:00 PM

 > 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
marinersk@earthlink.net
http://home.earthlink.net/~marinersk/
http://www.whirlyjigmusic.com/

0
Reply marinersk (16) 3/28/2005 7:30:04 PM

 > "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
marinersk@earthlink.net
http://home.earthlink.net/~marinersk/
http://www.whirlyjigmusic.com/

0
Reply marinersk (16) 3/28/2005 7:32:20 PM

4 Replies
45 Views

(page loaded in 0.071 seconds)

Similiar Articles:













8/1/2012 7:03:50 AM


Reply: