开发者

how to use localtime function with VIsual Studio C++ 2008

I am getting this error : "Unhandled exception at 0x00411690 in tim.exe: 0xC0000005: Access violation reading location 0x00000008" when I execute a program this is compiled and linked successfully and the problem is that localtime() function is not correctly recognized by Visual C++ 2008. (With VC++6, this program works fine).

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

} 

Is there any idea and thanks for replies:


The documentation says:

Return a pointer to the structure result. If the value in timer represents a date before midnight, January 1, 1970, return NULL.

So, verify that the time value you're sending in is correct. It seems a bit scary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜