c - difftime 在 MinGW 中返回不正确的值

标签 c windows time mingw time.h

我正在尝试计算两个 time_t 之间的差异。
但是 difftime 返回它的第一个参数而不是差值!
我的代码是:

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

 int main(){
     time_t etime_t,now_t;
     double time_diff;
     now_t=1388525484L;
     etime_t=1389338644L;
     time_diff=difftime(now_t,etime_t);
     printf("%f",time_diff);
 }

然后打印:

1388525484.000000

我正在使用 GCC 进行编译(在 MinWG 中)
有什么问题?

最佳答案

一个 MinGw 错误。他们将 difftime 编译为标准 Windows 函数的调用。然而,他们调用 difftime 的 32 位版本,即使参数是 64 位。这给出了预期的结果,因为它从其下半部分减去第一个参数的高半部分(即 0)。看 bug report这里。可以通过插入临时修复

#define _USE_32BIT_TIME_T 1

在包含time.h之前

关于c - difftime 在 MinGW 中返回不正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21015113/

相关文章:

c++ - OpenGL Programming Guild 第八版,示例程序和 'NumVAOs'

c - 程序的输出是无限的。为什么?

c++ - 在 Windows 上制作期间未定义 Qt vulkan 类

Java抽象类型类编译时检查

postgresql - 无法从时间戳 Postgres 中提取时间

澄清 Gamma 主题(一般)

c - (fscanf(file, "%lf", &num) > 0) 和 C 中的段错误

windows - 如何从启动中删除 Docker.Service

c++ - 在使用 WinCrypt 证书进行身份验证的 Windows 上创建 SSL 套接字?

php - 日期格式 ISO 8601