Linux next time output format is accurate to microsecond-gettimeofday__linux

Source: Internet
Author: User


<span style= "FONT-SIZE:18PX; BACKGROUND-COLOR:RGB (240, 240, 240); >
#include <sys/time.h></span>
int gettimeofday (struct TIMEVAL*TV, struct timezone *tz);
The parameter TV is the structure that holds the result of the time, and the parameter tz is used to save the time zone result:
struct timezone{
int tz_minuteswest;/* GMT to the West, Jet lag * *
int Tz_dsttime /*DST Time Correction mode */
}
timezone parameter if not used then pass in null.
The structure body timeval is defined as:
struct timeval{
long int tv_sec;//sec number
long int tv_usec;//microsecond number
}
The time it obtains is accurate to microsecond (1e-6 s) magnitude. Use Gettimeofday before and after a piece of code to calculate the code execution time:
struct timeval tv_begin, tv_end;
Gettimeofday (&tv_begin, NULL);
Foo ();
Gettimeofday (&tv_end, NULL);</span>

Time format for output refinement to microseconds

</pre><pre name= "code" class= "CPP" >
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <time.h> /
*
Take the current time, accurate to microseconds;
*
/int main ()
{
    struct timeval TV;
	struct TM *     time_ptr;
    memset (&TV, 0, sizeof (timeval));
    Gettimeofday (&TV, NULL);
    Time_ptr = LocalTime (&tv.tv_sec);
     printf ("%d-%02d-%02d%02d:%02d:%02d.%.04d\n",
            time_ptr->tm_year + 1900,
            Time_ptr->tm_mon + 1,
            Time_ptr->tm_mday,
            Time_ptr->tm_hour,
            time_ptr->tm_min,
            time_ptr->tm_sec,
			TV.TV_USEC);
    return 1;
} </span>


Related Article

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.