我想在 windows 和 linux 中使用以下函数,但我不确定如何将 __int64 转换为 unsigned long。像我一样投射价值是否安全?
getTimeInMilliseconds()
{
#ifdef _WIN32
static const __int64 magic = 116444736000000000; // 1970/1/1
SYSTEMTIME st;
GetSystemTime(&st);
FILETIME ft;
SystemTimeToFileTime(&st,&ft); // in 100-nanosecs...
__int64 t;
memcpy(&t,&ft,sizeof t);
return (unsigned long)((t - magic)/10000);
#else
struct timeval tv;
gettimeofday(&tv, NULL);
unsigned long s = tv.tv_sec * 1000;
unsigned long us = tv.tv_usec / 1000;
return s + us;
#endif
}