我想看看在 1970 年 1 月 1 日的 00:00:00 是否真的对应于 0 秒,我写了以下内容:
#include <stdio.h>
#include <time.h>
int main(void) {
int year = 1970;
struct tm t = {0};
t.tm_mday = 1; // January
t.tm_year = year - 1900;
t.tm_hour = 0;
t.tm_isdst = -1;
printf("%ld\n", mktime(&t));
return 0;
}
它给了我 -3600 的值。我哪里错了?PS:使用 GCC v.10.1 测试。我尝试在另一个架构下使用另一个编译器,它给了我正确的值。
最佳答案
mktime
函数需要在本地时间。显然,您本地时间的 00:00:00 是纪元前一小时。使用 TZ
启动程序设置为 UTC
.
关于c - 为什么 mktime 少给我一个小时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62867284/