You cannot do this portable. time_t is not even an integral type. It can be a structure or something really.
Since it looks like you are setting the tv_sec member struct timeval, why don't you just use the correct type? This is defined as long int, so you should do:
tv.tv_sec = std::numeric_limits<long int>::max();
Although, now I notice that POSIX says tv_sec is time_t, although MSDN and GNU libc use a long int. These old time APIs, which cannot even distinguish between durations and time points, should definitely be replaced. If you can use C ++ 11 check out std :: chrono.
In any case, std::numeric_limits<std::time_t>::max() really gives the maximum value of time_t even on Windows. The only problem is that the Window definition for timeval :: tv_sec is not time_t as a POSIX mandate, and so casting to long int gets a negative number.
However, this can be fixed on Windows by specifying _USE_32BIT_TIME_T. This will cause the time_t definition to match the type used in timeval for tv_sec, and then your std::numeric_limits<time_t>::max() will work.
source share