我收到此错误:“tim.exe 中 0x00411690 处未处理的异常:0xC0000005:访问冲突读取位置 0x00000008”,当我执行已成功编译和链接的程序时,问题是 localtime() 函数不正确被 Visual C++ 2008 识别。(使用 VC++6,这个程序工作正常)。
...
int gettimeofday(struct timeval *tv, struct timezone *tz)
{
FILETIME ft;
unsigned __int64 tmpres = 0;
static int tzflag = 0;
if (NULL != tv)
{
GetSystemTimeAsFileTime(&ft);
tmpres |= ft.dwHighDateTime;
tmpres <<= 32;
tmpres |= ft.dwLowDateTime;
tmpres /= 10; /*convert into microseconds*/
/*converting file time to unix epoch*/
tmpres -= DELTA_EPOCH_IN_MICROSECS;
tv->tv_sec = (long)(tmpres / 1000000UL);
tv->tv_usec = (long)(tmpres % 1000000UL);
}
if (NULL != tz)
{
if (!tzflag)
{
_tzset();
tzflag++;
}
tz->tz_minuteswest = _timezone / 60;
tz->tz_dsttime = _daylight;
}
return 0;
}
uint32_t stampstart()
{
struct timeval tv;
struct timezone tz;
struct tm *tm;
uint32_t start;
gettimeofday(&tv, &tz);
tm = localtime(&tv.tv_sec); /////--- problem is here ---
printf("TIMESTAMP-START\t %d:%02d:%02d:%d (~%d ms)\n", tm->tm_hour,
tm->tm_min, tm->tm_sec, tv.tv_usec,
tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 +
tm->tm_sec * 1000 + tv.tv_usec / 1000);
start = tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 +
tm->tm_sec * 1000 + tv.tv_usec / 1000;
return (start);
}
有什么想法,谢谢回复:
最佳答案
Return a pointer to the structure result. If the value in timer represents a date before midnight, January 1, 1970, return NULL.
因此,请验证您发送的时间值是否正确。好像有点吓人。
关于c++ - 如何在 VIsual Studio C++ 2008 中使用本地时间函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2493627/