I was trying out the examples in expert C programming while I encountered this problem. My program basically does one thing: use the standard gmtime
function and see how many years has past since 1970.
Here is my program:
#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;
}
The output is 117, the number of years past 1900. This is unexpected because I checked time()
and man gmtime
beforehand and they both said they are relative to the Epoch time (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).
According to the above description, my program should have returned 47 instead of 117. What is going on here?
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)
tm_year The number of years since 1900.
– Reporttm_year
. It describes how to interpret thetime_t
value, which is the argument togmtime(3)
. – Emulationtm_year
is need to isolate the year in this century subtract 100. If need 2022 - can convert to string + "20" and back to integer as needed. – Bowie