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 (&TV);
Return ((time_t) tv.tv_sec* (time_t) 1000000+tv.tv_usec);
}
#endif
Next, give the
How to use :
Timeval TV;
Gettimeofday (&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);