C + + get UTC time accurate to microsecond implementation code _c language

Source: Internet
Author: User
Time-class functions are often used in the daily development process, where the UTC time of 1970 is more commonly used, but there are no functions that are directly accurate to the delicate level under Windows. This article provides a way to solve this type of demand problem.

Here is the first C + + implementation code
Copy Code code as follows:

#ifndef Utc_time_stamp_h_
#define Utc_time_stamp_h_

#include <windows.h>
#include <sys/timeb.h>
#include <time.h>

#if!defined (_winsock2api_) &&!defined (_winsockapi_)
struct Timeval
{
Long tv_sec;
Long tv_usec;
};
#endif

static int gettimeofday (struct timeval* TV)
{
Union {
Long Long ns100;
FILETIME ft;
} now;
Getsystemtimeasfiletime (&now.ft);
Tv->tv_usec = (long) ((NOW.NS100/10LL)% 1000000LL);
Tv->tv_sec = (long) ((NOW.NS100-116444736000000000LL)/10000000LL);

return (0);
}
Get the subtle number of UTC since 1970
Static time_t timeconversion::getutccaressing ()
{
Timeval TV;
Gettimeofday (&AMP;TV);
Return ((time_t) tv.tv_sec* (time_t) 1000000+tv.tv_usec);
}
#endif

Next, give the How to use :
Timeval TV;
Gettimeofday (&AMP;TV);
or direct call: Getutccaressing ();

Final Note:This code is tested under VS2008 and VS2010, and can be safely used
Appendix: This article also gives the UTC time second level UTC Get method Code:
Copy Code code as follows:

time_t TIMEP;
struct TM *p;
Time (&TIMEP);
P=localtime (&TIMEP);
TIMEP = Mktime (p);
printf ("%d\n", TIMEP);

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.