我正在尝试使用专家C编程中的示例,同时遇到此问题。我的程序基本上是做一件事情:使用标准gmtime
功能,看看有多少岁月已经过去自1970年以来 这里是我的程序:为什么结构tm中的tm_year成员相对于1900年而不是1970年的macosx上的C?
#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;
}
输出为,几年过去数。这是出乎意料的,因为我查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).
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).
根据以上描述,我的程序应该返回而不是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)
因为这就是[doc说](http://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html)也是[在线手册](http://www.cplusplus。com/reference/ctime/tm /) – litelite
另外手册页:'tm_year 1900年以来的年数.' – Mat
您引用的ctime(3)手册页的部分没有提及有关'tm_year'的任何内容。它描述了如何解释'time_t'的值,它是'gmtime(3)'的*参数*。 – Ssswift