In the daily development process, the statistics of time functions are often used. It is often used to obtain the UTC time from January 1, 1970 to the present, however, in windows, there is no function available that can be precise to a subtle level. This document provides a solution to this type of problem.
Next we will first giveC ++ implementationCode:Copy codeThe Code is 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)/0000000ll );
Return (0 );
}
// Obtain the number of seconds since January 1, 1970 in UTC
Static time_t timeconversion: getutccaressing ()
{
Timeval TV;
Gettimeofday (& TV );
Return (time_t) TV. TV _sec * (time_t) 1000000 + TV. TV _usec );
}
# Endif
Next we will provideUsage:
Timeval TV;
Gettimeofday (& TV );
Or call: getutccaressing ();
Note:The code in this article has been tested in both vs2008 and vs2010, so you can use it with confidence.
Appendix: This document also provides the code for obtaining the method in seconds of UTC:Copy codeThe Code is as follows: time_t timep;
Struct TM * P;
Time (& timep );
P = localtime (& timep );
Timep = mktime (P );
Printf ("% d \ n", timep );