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


#include "tsk_time.h"



#include "tsk_debug.h"


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


#include <time.h>
#if defined (__APPLE__)
# include <mach/mach.h>
# include <mach/mach_time.h>
#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);
}

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐