Linux5 get time functions

Source: Internet
Author: User

# Include <sys/time. h> <br/> # include <time. h> <br/> # include <stdio. h> <br/> # include <iostream> <br/> # include <string. h> <br/> using namespace std; <br/> double difftimeval (const struct timeval * tv1, const struct timeval * tv2) <br/>{< br/> double d; <br/> time_t s; <br/> suseconds_t u; <br/> s = tv1-> TV _sec-tv2-> TV _sec; <br/> u = tv1-> TV _usec-tv2-> TV _usec; <br/> if (u <0) <br/> -- s; <br/> d = s. <br/> d * = 1000000.0; <br/> d + = u; <br/> return d; <br/>}< br/> char * strftimeval (const struct timeval * TV, char * buf) <br/>{< br/> struct tm; <br/> size_t len = 28; <br/> localtime_r (& TV-> TV _sec, & tm ); <br/> strftime (buf, len, "% Y-% m-% d % H: % M: % S", & tm ); <br/> len = strlen (buf); <br/> sprintf (buf + len ,". % 06.6d ", (int) (TV-> TV _usec); <br/> return buf; <br/>}< br/> char * getstimeval (char * buf) <br/>{< br/> struct timeval TV; <br/> struct tm; <br/> size_t len = 28; <br/> gettimeofday (& TV, NULL); <br/> localtime_r (& TV. TV _sec, & tm); <br/> strftime (buf, len, "% Y-% m-% d % H: % M: % S", & tm ); <br/> len = strlen (buf); <br/> sprintf (buf + len ,". % 06.6d ", (int) (TV. TV _usec); <br/> return buf; <br/>}< br/> // get system time <br/> void SendCentreTime (char * sCurDate) <br/>{< br/> struct tm * current_date; <br/> time_t seconds; <br/> time (& seconds ); <br/> current_date = localtime (& seconds); <br/> memset (sCurDate, 0, sizeof (sCurDate); <br/> sprintf (sCurDate, "% 04d % 02d % 02d % 02d % 02d % 02d", <br/> current_date-> tm_year + 1900, <br/> current_date-> tm_mon + 1, <br/> current_date-> tm_mday, <br/> current_date-> tm_hour, <br/> current_date-> tm_min, <br/> current_date-> tm_sec ); <br/>}< br/> long GetMillSec () <br/>{< br/> long nMillSec = 0; </p> <p> struct timeval TV; <br/> gettimeofday (& TV, NULL); </p> <p> nMillSec = (long) TV. TV _sec * 1000; <br/> nMillSec + = TV. TV _usec/1000; </p> <p> return nMillSec; <br/>}< br/> int main (int argc, char * argv []) <br/>{< br/> char buf [28]; <br/> char cDate [128] = {0}; <br/> struct timeval tv1; <br/> struct timeval tv2; </p> <p> long tm = GetMillSec (); <br/> SendCentreTime (cDate ); <br/> gettimeofday (& tv1, NULL); <br/> printf ("% s/n", getstimeval (buf); <br/> gettimeofday (& tv2, NULL); <br/> printf ("% s/n", strftimeval (& tv1, buf); <br/> printf ("% s/n ", strftimeval (& tv2, buf); <br/> printf ("% f/n", difftimeval (& tv2, & tv1); <br/> return 0; <br/>}< br/> 

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.