From glibc's implementation we read:
We limit the size of the year which can be printed. Using the %d
format specifier used the addition of 1900 would overflow the
number and a negative vaue is printed. For some architectures we
could in theory use %ld or an evern larger integer format but
this would mean the output needs more space. This would not be a
problem if the 'asctime_r' interface would be defined sanely and
a buffer size would be passed.
Run the program below to find the exact limit on your machine.
#include <limits.h>
#include <stdio.h>
#include <time.h>
/**
* Find the largest time_t for which ctime returns a non-NULL value
* using a bsearch between 0 and LONG_MAX.
**/
static time_t ctime_max() {
time_t start = 0, end = LONG_MAX, mid;
while (start < end) {
mid = start + (end - start) / 2;
if (ctime(&mid)) {
/* this mid is ctime-able; try higher */
start = mid + 1;
} else {
/* this mid is not ctime-able; try lower */
end = mid;
}
}
/* mid is now the lowest number that's too high; subtract one */
return mid - 1;
}
int main() {
time_t t = ctime_max();
printf("%s", ctime(&t));
return 0;
}
For me that comes out to Tue Dec 31 23:59:59 2147483647
which happens to be the second before the year overflows four signed bytes.