If the user type time_t is defined as __darwin_time_t, which itself is defined as long in MacOS X, why does the following code outputs 8 Time is (null)? Maybe it's something silly, but I can't really understand it.
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t time = 0x7FFFFFFFFFFFFFFF;
printf("%lu\n"
"Time is %s\n", sizeof(time_t), ctime(&time));
return 0;
}
Time 0x7FFFFFFFFFFFFFFF appears to be around the year 292,471,210,647 AD, which undoubtedly causes ctime to exceed the 26 characters it is guaranteed by C99, so it returns NULL rather than overflowing its buffer. In general, try to avoid any dates that occur after the Morlocks go to war with the Eloi.
While working through the book "Expert C Programming", I ran across the same problem in Lion 10.7.3 -- with t=0xf0c00000000000, ctime(&t) yields Wed Mar 1 21:07:12 214739252 and with t=0xf0d00000000000, ctime(&t) returns the null pointer (0x0).
So it doesn't appear to be a wrap around for t, but some test inside ctime(&t) that returns the null pointer if t is too large.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With