Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ctime returning null

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;
}
like image 611
sidyll Avatar asked Apr 19 '26 21:04

sidyll


2 Answers

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.

like image 79
jwodder Avatar answered Apr 22 '26 11:04

jwodder


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.

like image 43
dgktkr Avatar answered Apr 22 '26 09:04

dgktkr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!