Elasped Time - in hr, min and seconds

Discussion in 'C Programming' started by Neo, Mar 28, 2005.

  1. Neo

    Neo Guest

    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
     
    Neo, Mar 28, 2005
    #1
    1. Advertising

  2. 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
     
    Gregory Toomey, Mar 28, 2005
    #2
    1. Advertising

  3. Neo

    CBFalconer Guest

    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
     
    CBFalconer, Mar 28, 2005
    #3
  4. > 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

    http://home.earthlink.net/~marinersk/
    http://www.whirlyjigmusic.com/
     
    Steven K. Mariner, Mar 28, 2005
    #4
  5. > "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

    http://home.earthlink.net/~marinersk/
    http://www.whirlyjigmusic.com/
     
    Steven K. Mariner, Mar 28, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robocop
    Replies:
    3
    Views:
    7,119
    Sean Berry
    Feb 26, 2004
  2. Lois
    Replies:
    1
    Views:
    3,372
    Ryan Stewart
    Dec 27, 2004
  3. Summu82
    Replies:
    5
    Views:
    890
    Richard Heathfield
    Jun 7, 2006
  4. carmen

    Converting hrs and min to just min

    carmen, Aug 10, 2004, in forum: ASP General
    Replies:
    4
    Views:
    142
    carmen
    Aug 10, 2004
  5. Michael Tan
    Replies:
    32
    Views:
    1,051
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page