【发布时间】:2011-09-14 19:10:04
【问题描述】:
我想在 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
}
【问题讨论】:
-
在 Windows 中
__int64是 8 个字节宽,但unsigned long是 4 个字节长。这个问题没有答案。 -
是的,直到 2038 年 1 月 19 日星期二 03:14:07 UTC。
-
正好在我生日那天 :) 我希望到 2038 年 Windows 将早已不复存在。
-
@Vlad 您希望在 2038 年之前摆脱 *nix。Y2K38 错误是 *nix 问题而不是 Windows 问题! ;-)
-
@David:对不起,我误解了你的“没有解决方案”。事实上,我指出解决方案存在,但在另一个“域”中。