如何使用函数gettimeofday()或等值的东西与Visual Studio C ++ 2008? [英] how to use gettimeofday() or something equivalent with Visual Studio C++ 2008?

查看:1923
本文介绍了如何使用函数gettimeofday()或等值的东西与Visual Studio C ++ 2008?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能有人请帮我用函数gettimeofday()函数与Visual Studio C ++ 2008在Windows XP?这里是一个code,我发现某处在网络上:

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:

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

当我测试code。与VC ++ 6去年,它工作得很好。但现在因为我用VC ++ 2008,我得到的异常处理错误。那么,有没有关于如何使用gettimeofday的东西或相当于任何想法?

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?

感谢您的回复和任何帮助将是非常美联社preciated:

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

推荐答案

在UNIX使用时区结构的已经过时了。我不知道你为什么使用它。见<一href=\"http://linux.about.com/od/commands/l/blcmdl2_gettime.htm\">http://linux.about.com/od/commands/l/blcmdl2_gettime.htm但是,如果你想用这个结构来了解GMT(UTC)探源从本地时间将是下一个:tz_minuteswest是从格林尼治标准​​时间(UTC)的分钟真正的探源和tz_dsttime是一个标志,指示是否现在白天在使用

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.

您有一些变化的例子正常工作在Visual C ++ 2008例preSS:

Your example with some changes works fine in 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;
}

这篇关于如何使用函数gettimeofday()或等值的东西与Visual Studio C ++ 2008?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆