What is the greatest useful value of time_t?

It seems surprisingly hard to figure out what is the valid range of values ​​for time_t .

It is 32 bit on some platforms, on most 64 bits, and therefore it can be easily installed on LONG_MAX . However, trying to use this value does not really work as expected. For example, you cannot pass it to localtime and change it to struct tm .

A quick test program for binary value search tells me that it is 67768036191676799. This corresponds to the end of the year 2147483647, so it makes sense as a value. But is this stated anywhere, and is there a reasonable, platform-independent value for maximizing the use of time_t?

+2
source share
3 answers

Indeed, the specification for time_t and clock_t is determined by the implementation (C99 7.23.1).

This is one of those things in which I would recommend not generating these values ​​yourself, but relying on an implementation to create them for you, for example, using mktime() , and using struct tm to directly manipulate time. -1 is the only time_t value that is a "good" value that you can use yourself.

I would prefer that you do not consider it as a 32-bit value of any type, as jgm suggests. You just don’t know if some strange built-in compiler wants to use 16-bit time or 18 or who knows.

+1
source

The safest way to use it is with a 32-bit subscription if you are happy that it does not work after 25 years.

Otherwise, you will have to test the type yourself on any platforms on which you run and act accordingly.

0
source

tm_year is of type int , so if you convert to struct tm , the most meaning of time_t is the value corresponding to the year INT_MAX .

0
source

All Articles