- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
有人可以帮我在 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/
#include #include int main() { float time; struct timeval tv; gettimeofday( &tv, NULL );
gettimeofday根据 this page 是 x86-86 的系统调用(只需在框中搜索 gettimeofday): int gettimeofday(struct timeval *tv,
我需要使用函数 gettimeofday 进行家庭作业,在阅读手册页并查看一些在线示例后,我不明白为什么人们有时同时使用结构的 tv_sec 成员和结构的 tv_usec 成员。 手册页指出: T
如果在我测量间隔时发生时间变化,会发生什么情况,例如: gettimeofday(&start, NULL); system("./anotherProgram"); // during the
是否有任何方法可以在不使用任何数组的情况下存储 gettimeofday() 中的时间刻度数并将其格式化为某种特定方式(例如“%m-%d-%Y %T”)? 这将为计算当前时间的程序的每个实例节省内存。
当我输出 gettimeofday() 的微秒字段时,我注意到微秒字段大于 1,000,000。有人知道为什么吗?这是否意味着我对 gettimeofday() 的解释是错误的? 郑重声明,我的假设是
我正在用 c 编写一些基本的东西,计算执行 shell 脚本所需的时间。 我有 gettimeofday(&start, NULL); // code here gettimeofday(&end,
我想获取一个线程进入临界区和另一个线程获得进入 ARM CortexA8 上同一临界区的权限之间耗时。为此,我一直在使用 C 语言中的 gettimeofday() 函数。 void *Thre
对于以下代码,我有时会得到负值。我不明白这一点。任何人都可以解释为什么会发生这种情况。 int64_t gettimelocal() { struct timeval Time; if
/* * Returns time in s.usec */ float mtime() { struct timeval stime; gettimeofday(&stime,0
我为我所在大学的抽样作业编写了这段代码。 #include #include #include #include int main(int argc, char **argv){ st
我正在尝试以十分之一秒的精度打印 ISO-8601 中的时间。YYYY-MM-DDThh:mm:ss.s 这是我的代码: #include #include #include #include
我有一个程序可以计算发布-订阅模型中对象的延迟时间。我为时间戳使用了以下函数: uint64_t GetTimeStamp() { struct timeval tv; gettime
我试过谷歌、php.net 和 php 邮件列表的存档,但我找不到我在找什么。也许这是显而易见的,或者也许没有人想知道这…… 多年来,我一直使用 microtime() 来获取当前时间,包括微秒。然而
如何将此功能从 linux 转换到 Windows?我不能使用 gettimeofday 函数 double getSysTime() { struct timeval tp; getti
我正在编写一个线程库,在安排线程时我需要知道它们已经准备了多长时间。每个 Thread 实例都有一个 timeval _timeInReady 字段,当我将一个实例推送到就绪队列时,我调用这个函数:
我正在将最初为 Win32 API 编写的游戏移植到 Linux(嗯,将 Win32 端口的 OS X 端口移植到 Linux)。 我已经实现了 QueryPerformanceCounter,方法是
我正在尝试计算系统调用的平均开销,因此我重复执行 0 字节读取系统调用,并将平均开销计算为时间差除以迭代次数。但是,有时当我这样做时,我会得到一个负数。这是我的代码: #include #inclu
我正在做一个涉及比较编程语言的项目。我正在计算阿克曼函数。我测试了 Java、Python 和 Ruby,得到了 10 到 30 毫秒之间的响应。但是 C++ 似乎需要 125 毫秒。这是正常的,还是
我正在尝试使用 gettimeofday 或 cudaEventRecord 来计时循环。然而,他们报告的结果截然不同。这是伪代码: // get time here (start) whil
我是一名优秀的程序员,十分优秀!