c++ - 如何在 VIsual Studio C++ 2008 中使用本地时间函数

标签 c++ c visual-studio-2008 visual-c++ exception

我收到此错误:“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); 

} 

有什么想法,谢谢回复:

最佳答案

documentation说:

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/

相关文章:

c++ - 从 N9 上的 Apps Screen 恢复 MeeGo Harmattan Qt 应用程序

c++ - 晚声明变量是不是更有效率?

c - 打印的值前面有不同数量的空格吗?

database - 如何在 Visual Studio 2008 中连接到 Access 2007 (accdb) 文件

visual-studio-2008 - 奇怪的 MFC/VC++ 链接器错误(std::list<CRect> 已定义)

winforms - .NET/Visual Studio 编辑器中的相对控件位置

c++ - 什么是 INT_PTR 以及如何将数据转换为 INT_PTR?

c++ - 为什么在 qtablewidget 的第一列中设置值时 gui 崩溃

c++ - 无法创建DLL : Getting DLL "is not a valid Win32 application"

c - 在主函数内部静态分配大量内存