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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2494356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:54:49  来源:igfitidea点击:

how to use gettimeofday() or something equivalent with Visual Studio C++ 2008?

cvisual-studiovisual-studio-2008visual-c++gettimeofday

提问by make

Could someone please help me to use gettimeofday() function with Visual Studio C++ 2008 on Windows XP? here is a code that I found somewhere on the net:

有人可以帮我在 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); 

Last year when I tested this code with VC++6, it worked fine. But now as I use VC++ 2008, I am getting error of exception handling. So is there any idea on how to use gettimeofday or something equivalent?

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

Thanks for your reply and any help would be very appreciated:

感谢您的回复,任何帮助将不胜感激:

回答by Neokoder

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.htmBut 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.

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

Your example with some changes works fine in Visual C++ 2008 Express:

您的一些更改示例在 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;
}

回答by Philm

Simple stopwatch in seconds

秒表简单秒表

FWIW: This is a similar one as that of Kate, but I just wanted to mention it, ifsomeone is looking for the most simple stopwatch in C++ (counting seconds). Not a big deal, I know. It has only resolution of 1 sec, so if you want to cound microsecs, go on with the other examples.

FWIW:这与 Kate 的相似,但我只想提一下,如果有人在 C++ 中寻找最简单的秒表(计数秒)。没什么大不了的,我知道。它只有 1 秒的分辨率,因此如果您想计算微秒,请继续其他示例。

double seconds=0;
time_t timer1, timer2;
time(&timer1);  /* get current time  */
...
time(&timer2);  /* get current time  later */
seconds = difftime(timer2,timer1);

回答by Kate Gregory

There are a few different types to represent a time. Here's some code I used recently:

有几种不同的类型来表示时间。这是我最近使用的一些代码:

time_t now;
tm* local;
time(&now); 
local=localtime(&now);

I then went on to build a string from pieces of local, but you could do what you wanted at this point.

然后我继续从local.

Kate

凯特

回答by Areeha

In my case it worked as following: Since I wanted to find the average running time, i used: First include in the beginning of the program, then: initialize samples=100 (for example);

在我的情况下,它的工作方式如下:因为我想找到平均运行时间,我使用:首先包含在程序的开头,然后:初始化样本=100(例如);

time_t t_start,t_end;
time(&t_start); //get the current time

//program that needs to be timed

time(&t_end); //get the time at the end of execution of the program
seconds = (difftime(t_start,t_end))/samples;//calculate the difference