gettimeofday windows上的实现

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);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值