Find the answer to your Linux question:
Results 1 to 3 of 3
Hi there, I am having problem understanding the result of gettimeofday() call. In my understanding, it returns a struct of timeval, which has the definition Code: struct timeval { time_t ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2010
    Posts
    7

    gettimeofday() format


    Hi there,

    I am having problem understanding the result of gettimeofday() call. In my understanding, it returns a struct of timeval, which has the definition
    Code:
    struct timeval {
        time_t      tv_sec;     /* seconds */
        suseconds_t tv_usec;    /* microseconds */
    };
    The tv_sec part of the return value represents the seconds part since the Epoch, and tv_usec represents the microsecond part, meaning that these two parts are sort of irrelevant, right? However, if I do a call like this:
    Code:
    struct timeval tv;
    gettimeofday(&tv);
    printf("%u.%u \n",&tv.tv_sec,&tv.tv_usec);
    which, by the way, is from one example from online. The result is beyond my understanding that the tv_usec part is always larger than the tv_sec part by 4. For example, one output would be: 3216945432.3216945436 But like I said, should these two parts be irrelevant? Or is there any misunderstanding? Thanks for any help!

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,598
    Remove the & from &tv.tv_sec and &tv.tv_usec in the printf() call. In order to convert the tv.tv_sec to a sensible date+time value you would use one of the related functions, such as ctime(). See you system man pages for more details.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Oct 2010
    Posts
    7
    Quote Originally Posted by Rubberman View Post
    Remove the & from &tv.tv_sec and &tv.tv_usec in the printf() call. In order to convert the tv.tv_sec to a sensible date+time value you would use one of the related functions, such as ctime(). See you system man pages for more details.
    Thanks Rubber,

    Now it works. It was such a stupid problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •