gettimeofday windows上的实现

gettimeofday是Linux上的函数,在windows的实现,这里直接转doubango工程中的tsk_time.c 源文件种的实现,可以参考;


#include "tsk_time.h"



#include "tsk_debug.h"


#if TSK_UNDER_WINDOWS
# include // timeval
# include
#elif defined(__SYMBIAN32__)
# include <_timeval.h> 
#else
# include
#endif


#include
#if defined (__APPLE__)
# include
# include
#endif


/**@defgroup tsk_time_group Datetime functions.
*/


#if !HAVE_GETTIMEOFDAY
#if TSK_UNDER_WINDOWS


/* For windows implementation of "gettimeofday" Thanks to "http://www.cpp-programming.net/c-tidbits/gettimeofday-function-for-windows" */
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
#else
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
#endif


struct timezone 
{  
int  tz_minuteswest; // minutes W of Greenwich  
int  tz_dsttime;     // type of dst correction
};


int gettimeofday(struct timeval *tv, struct timezone *tz) 
{  
FILETIME ft;
uint64_t tmpres = 0;  
static int tzflag = 0; 


if(tv)   
{    
#ifdef _WIN32_WCE
SYSTEMTIME st;
GetSystemTime(&st);
SystemTimeToFileTime(&st, &ft);
#else
GetSystemTimeAsFileTime(&ft);
#endif


tmpres |= ft.dwHighDateTime;   
tmpres <<= 32; 
tmpres |= ft.dwLowDateTime;


/*converting file time to unix epoch*/   
tmpres /= 10;  /*convert into microseconds*/  
tmpres -= DELTA_EPOCH_IN_MICROSECS;  
tv->tv_sec = (long)(tmpres / 1000000UL); 
tv->tv_usec = (long)(tmpres % 1000000UL); 
}


if (tz){   
if (!tzflag){
#if !TSK_UNDER_WINDOWS_RT
_tzset();
#endif
tzflag++;  
}   
tz->tz_minuteswest = _timezone / 60;
tz->tz_dsttime = _daylight;
}


return 0; 
}


#else
#pragma error "You MUST provide an implement for 'gettimeofday'"
#endif /* WIN32 */


#endif /* !HAVE_GETTIMEOFDAY */


/**@ingroup tsk_time_group
* The tsk_gettimeofday() function shall obtain the current time, expressed as seconds and microseconds since EPOCH (00:00:00 UTC on 1 January 1970). 
* The resolution of the system clock is unspecified.
* @param tv The current time, expressed as seconds and microseconds since EPOCH(00:00:00 UTC on 1 January 1970).
* @param tz The timezone.
* @retval The tsk_gettimeofday() function shall return 0 and no value shall be reserved to indicate an error.
*/
int tsk_gettimeofday(struct timeval *tv, struct timezone *tz)
{
return gettimeofday(tv, tz);
}


/**@ingroup tsk_time_group
*/
uint64_t tsk_gettimeofday_ms()
{
struct timeval tv;
tsk_gettimeofday(&tv, tsk_null);
return (((uint64_t)tv.tv_sec)*(uint64_t)1000) + (((uint64_t)tv.tv_usec)/(uint64_t)1000);
}


/**@ingroup tsk_time_group
* Gets the number of milliseconds in @a tv
* @retval The number of milliseconds
*/
uint64_t tsk_time_get_ms(const struct timeval* tv)
{
if(!tv){
TSK_DEBUG_ERROR("Invalid parameter");
return 0;
}
return (((uint64_t)tv->tv_sec)*(uint64_t)1000) + (((uint64_t)tv->tv_usec)/(uint64_t)1000);

}





这个是网络上常见的实现;
int gettimeofday(struct timeval *tp, void *tzp)
{
time_t clock;
struct tm tm;
SYSTEMTIME wtm;


GetLocalTime(&wtm);
tm.tm_year     = wtm.wYear - 1900;
tm.tm_mon     = wtm.wMonth - 1;
tm.tm_mday     = wtm.wDay;
tm.tm_hour     = wtm.wHour;
tm.tm_min     = wtm.wMinute;
tm.tm_sec     = wtm.wSecond;
tm. tm_isdst    = -1;
clock = mktime(&tm);
tp->tv_sec = clock;
tp->tv_usec = wtm.wMilliseconds * 1000;


return (0);
}

你可能感兴趣的:(网络)