struct Timeval structure body and gettimeofday () function __ function

Source: Internet
Author: User
Tags epoch time function prototype
The definition of struct timeval structure of struct timeval structure in time.h is as follows:
struct Timeval
{
__time_t tv_sec; /* Seconds. */
__suseconds_t tv_usec; /* microseconds. */
}; Where Tv_sec is epoch to the number of seconds to create the struct timeval, and the tv_usec is the microsecond number, which is the fraction after the second. For example, when I write a blog post, the tv_sec is 1244770435,tv_usec 442388, that is, the current time epoch time 1,244,770,435 seconds, 442388 microseconds. It should be noted that, because of the cycle process, the new structure variables such as the process of consuming part of the time, we do the following operation will be the following results:
#include <sys/time.h>
#include <stdio.h>

Int
Main (void)
{
int i;
struct Timeval TV;

for (i = 0; i < 4; i++) {
Gettimeofday (&AMP;TV, NULL);
printf ("%d\t%d\n", Tv.tv_usec, tv.tv_sec);
Sleep (1);
}

return 0;
329612 1314851429 329782 1314851430 329911 1314851431 330036 1314851432 preceded by a microsecond number followed by a second number, you can see that in this simple operation, you can only pinpoint one to two digits behind the decimal point, Or you can see that each cycle takes 0.005 seconds, and using this program as a timer is obviously not going to work unless the resulting code consumes time.
Second, Gettimeofday () function prototype:/* Get the "current" and timezone information,
Putting it into *TV and *tz. If TZ is NULL and *tz is not filled.
Returns 0 on success,-1 on errors.
Note:this form of timezone information is obsolete.
Use the functions and variables declared in <time.h> instead. */
extern int gettimeofday (struct timeval *__restrict __tv,
__timezone_ptr_t __tz) __throw __nonnull ((1)); The Gettimeofday () function is to get the current time and the time zone, written to the TV and TZ respectively, and not to TZ if TZ is null.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.