繁体   English   中英

为什么在macosx上struct tm中的tm_year成员相对于1900而不是1970中的1970?

[英]Why is the tm_year member in struct tm relative to 1900 rather than 1970 in C on macosx?

遇到此问题时,我正在尝试使用专家C编程编写示例。 我的程序基本上做一件事:使用标准的gmtime函数,看看自1970年以来已经过去了多少年。这是我的程序:

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

int main(int argc, char** argv) {
    time_t now = time(0);
    struct tm *t = gmtime(&now);
    printf("%d\n", t->tm_year);
    return 0;
}

输出为117 ,即1900年之后的年数 这是意外的,因为我事先检查了time()man gmtime ,他们都说它们与纪元时间(1970-1-1 00:00:00)相关:

time() returns the time as the number of seconds since the Epoch,
1970-01-01 00:00:00 +0000 (UTC).

http://man7.org/linux/man-pages/man2/time.2.html

The ctime(), gmtime() and localtime() functions all take an argument of data type 
time_t, which represents calendar time.  When interpreted as an absolute time 
value, it represents the number of seconds elapsed since the Epoch, 1970-01-01 
00:00:00 +0000 (UTC).

http://man7.org/linux/man-pages/man3/ctime.3.html

根据上面的描述,我的程序应该返回47而不是117。这是怎么回事?

macos sierra 10.12.5
Darwin 16.6.0 Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; root:xnu-3789.60.24~6/RELEASE_X86_64 x86_64
Apple LLVM version 8.1.0 (clang-802.0.42)

所有 POSIX兼容平台上,不仅在macOS上, tm_year字段都相对于1900。

struct tm设计用于解析,显示和处理人类可读的日期。 创建日期时,通常会写日期,甚至存储日期时也不会包含年份数字中的“ 19”部分,并且距Y2K问题大约还有25年的距离。 因此,使tm_year相对于1900成为直接可两位数打印的便利,在当时似乎很合理。

Unix时间戳相对于“ Unix纪元”,即1970-01-01 00:00:00 UTC。 出于这个原因, 请参阅此问答

根据C库规范, struct tmtm_year成员相对于1900。 所有符合标准的库都使用该库。

tm结构至少应按任何顺序包含以下成员。 成员的语义及其正常范围在第7.27.2.1条注释中表示4

...
int tm_year; // years since 1900

time()返回time_t值“可以表示基于特定纪元的日历时间”。 这通常是世界标准时间1970年1月1日0:00:00。 * nix系统坚持这一点。 C不需要1月1日这一时期,并且与struct tmtm_year成员的时期没有直接的tm_year

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM