c - 如何使用 gettimeofday() 或与 Visual Studio C++ 2008 等效的东西?

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

有人可以帮我在 Windows XP 上使用 Visual Studio C++ 2008 的 gettimeofday() 函数吗?这是我在网上某处找到的代码:

#include < time.h >
#include <windows.h> 

#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
  #define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
#else
  #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
#endif

struct timezone 
{
  int  tz_minuteswest; /* minutes W of Greenwich */
  int  tz_dsttime;     /* type of dst correction */
};

int gettimeofday(struct timeval *tv, struct timezone *tz)
{
  FILETIME ft;
  unsigned __int64 tmpres = 0;
  static int tzflag;

  if (NULL != tv)
  {
    GetSystemTimeAsFileTime(&ft);

    tmpres |= ft.dwHighDateTime;
    tmpres <<= 32;
    tmpres |= ft.dwLowDateTime;

    /*converting file time to unix epoch*/
    tmpres -= DELTA_EPOCH_IN_MICROSECS; 
    tmpres /= 10;  /*convert into microseconds*/
    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;
}

...
// call gettimeofday()
 gettimeofday(&tv, &tz); 
 tm = localtime(&tv.tv_sec); 

去年我用 VC++6 测试这段代码时,它运行良好。但是现在当我使用 VC++ 2008 时,出现异常处理错误。那么对于如何使用 gettimeofday 或类似的东西有什么想法吗?

感谢您的回复,我们将不胜感激:

最佳答案

在 UNIX 中,时区结构的使用已过时。我不知道你为什么要用它。参见 http://linux.about.com/od/commands/l/blcmdl2_gettime.htm但是如果你想使用这个结构来了解 GMT(UTC) 与你本地时间的差异,它将是下一个:tz_minuteswest 是与 GMT(UTC) 的真正差异,以分钟为单位,而 tz_dsttime 是一个标志,指示现在是否正在使用日光.

您的示例经过一些更改后在 Visual C++ 2008 Express 中运行良好:

#include "stdafx.h"
#include <time.h>
#include <windows.h> 

const __int64 DELTA_EPOCH_IN_MICROSECS= 11644473600000000;

/* IN UNIX the use of the timezone struct is obsolete;
 I don't know why you use it. See http://linux.about.com/od/commands/l/blcmdl2_gettime.htm
 But if you want to use this structure to know about GMT(UTC) diffrence from your local time
 it will be next: tz_minuteswest is the real diffrence in minutes from GMT(UTC) and a tz_dsttime is a flag
 indicates whether daylight is now in use
*/
struct timezone2 
{
  __int32  tz_minuteswest; /* minutes W of Greenwich */
  bool  tz_dsttime;     /* type of dst correction */
};

struct timeval2 {
__int32    tv_sec;         /* seconds */
__int32    tv_usec;        /* microseconds */
};

int gettimeofday(struct timeval2 *tv/*in*/, struct timezone2 *tz/*in*/)
{
  FILETIME ft;
  __int64 tmpres = 0;
  TIME_ZONE_INFORMATION tz_winapi;
  int rez=0;

   ZeroMemory(&ft,sizeof(ft));
   ZeroMemory(&tz_winapi,sizeof(tz_winapi));

    GetSystemTimeAsFileTime(&ft);

    tmpres = ft.dwHighDateTime;
    tmpres <<= 32;
    tmpres |= ft.dwLowDateTime;

    /*converting file time to unix epoch*/
    tmpres /= 10;  /*convert into microseconds*/
    tmpres -= DELTA_EPOCH_IN_MICROSECS; 
    tv->tv_sec = (__int32)(tmpres*0.000001);
    tv->tv_usec =(tmpres%1000000);


    //_tzset(),don't work properly, so we use GetTimeZoneInformation
    rez=GetTimeZoneInformation(&tz_winapi);
    tz->tz_dsttime=(rez==2)?true:false;
    tz->tz_minuteswest = tz_winapi.Bias + ((rez==2)?tz_winapi.DaylightBias:0);

  return 0;
}


int _tmain(int argc, _TCHAR* argv[])
{
struct timeval2 tv;
struct timezone2 tz;
struct tm *tm1; 
time_t time1;

ZeroMemory(&tv,sizeof(tv));
ZeroMemory(&tz,sizeof(tz));

gettimeofday(&tv, &tz); // call gettimeofday()
time1=tv.tv_sec;
tm1 = localtime(&time1); 



 FILE *f;
 f=fopen("rez.txt","w");

 fprintf(f,"%04d.%02d.%02d %02d:%02d:%02d\n",1900+tm1->tm_year,1+tm1->tm_mon,tm1->tm_mday,tm1->tm_hour,tm1->tm_min,tm1->tm_sec);
 fprintf(f,"Diffrence between GMT(UTC) and local time=%d %s\n",tz.tz_minuteswest,"minutes");
 fprintf(f,"Is Daylight now=%s\n",tz.tz_dsttime?"Yes":"No");

 fclose(f);
 return 0;
}

关于c - 如何使用 gettimeofday() 或与 Visual Studio C++ 2008 等效的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2494356/

相关文章:

c - 程序通过C预处理器后,这个声明行会变成什么?

似乎无法使 C TCP 服务器-客户端通信正确

visual-studio - 如何开始一个过程然后立即将其暂停?

c - PostQuitMessage() 不会关闭我的应用程序?

c# - Visual Studio 中的 ANTLR 语法高亮 DSL

visual-studio - Visual Studio 中远程 GDB 调试器的输出

c++ - 在 Visual Studio 2008 中输入俄语字母时出错抛出控制台

c++ - 由于 WTTlog.DLL 导致的运行时错误?

c++ - const QString& 的显式模板特化导致未解析的外部

c - 尝试调用单独的函数时的未定义行为