The maximum value held by a time_t ?

C

Chris McDonald

Apologies if the answer is staring me in the face.

Is there a preferred way to define the maximum value that may be held in a
variable of type time_t ?

On the two common machine-types and operating systems that I use, using
an equally common compiler, time_t is defined to be a signed 32-bit long.
On these platforms I can, thus, use the constant INT32_MAX.

However, I'd feel more comfortable using a constant named TIME_T_MAX, or
similar. Is there any advice along these lines?

(More accurately, I'm really seeking the maximum value that the runtime
environment will ever return from time(NULL); )

Thanks,
 
B

Ben Pfaff

Chris McDonald said:
Is there a preferred way to define the maximum value that may be held in a
variable of type time_t ?

It's tricky.

In one project, I use something like the following. But it's
subject to a whole host of caveats. I could list some of them,
but I'm sure I'd miss others.

Anyway, you're welcome to this if you find it useful.

#include <limits.h>

/* Build-time assertion building block. */
#define BUILD_ASSERT__(EXPR) \
sizeof(struct { unsigned int build_assert_failed : (EXPR) ? 1 : -1; })

/* Build-time assertion for use in a declaration context. */
#define BUILD_ASSERT_DECL(EXPR) \
extern int (*build_assert(void))[BUILD_ASSERT__(EXPR)]

#define TYPE_IS_INTEGER(TYPE) ((TYPE) 1.5 == (TYPE) 1)
#define TYPE_IS_SIGNED(TYPE) ((TYPE) 0 > (TYPE) -1)
#define TYPE_VALUE_BITS(TYPE) (sizeof(TYPE) * CHAR_BIT - TYPE_IS_SIGNED(TYPE))
#define TYPE_MINIMUM(TYPE) (TYPE_IS_SIGNED(TYPE) \
? ~(TYPE)0 << TYPE_VALUE_BITS(TYPE) \
: 0)
#define TYPE_MAXIMUM(TYPE) (TYPE_IS_SIGNED(TYPE) \
? ~(~(TYPE)0 << TYPE_VALUE_BITS(TYPE)) \
: (TYPE)-1)

/* C allows floating-point time_t, but we don't support it. */
BUILD_ASSERT_DECL(TYPE_IS_INTEGER(time_t));

/* We do try to cater to unsigned time_t, but I want to know about it if we
* ever encounter such a platform. */
BUILD_ASSERT_DECL(TYPE_IS_SIGNED(time_t));

#define TIME_MAX TYPE_MAXIMUM(time_t)
#define TIME_MIN TYPE_MINIMUM(time_t)
 
J

jameskuyper

Chris said:
Apologies if the answer is staring me in the face.

Is there a preferred way to define the maximum value that may be held in a
variable of type time_t ?

On the two common machine-types and operating systems that I use, using
an equally common compiler, time_t is defined to be a signed 32-bit long.
On these platforms I can, thus, use the constant INT32_MAX.

However, I'd feel more comfortable using a constant named TIME_T_MAX, or
similar. Is there any advice along these lines?

(More accurately, I'm really seeking the maximum value that the runtime
environment will ever return from time(NULL); )

If (time_t)(-1) > 0, then (time_t)(-1) will be greater than any valid
value. Otherwise, there's no good portable way of determining the
range. Sorry!
 
C

CBFalconer

Chris said:
Apologies if the answer is staring me in the face.

Is there a preferred way to define the maximum value that may be held in a
variable of type time_t ?

On the two common machine-types and operating systems that I use, using
an equally common compiler, time_t is defined to be a signed 32-bit long.
On these platforms I can, thus, use the constant INT32_MAX.

That is a system that will have problems in 2038. However, by then
we expect most Unix, Linux, etcX systems will be using 64 bits for
a time_t.
 
I

Ian Collins

CBFalconer said:
That is a system that will have problems in 2038. However, by then
we expect most Unix, Linux, etcX systems will be using 64 bits for
a time_t.

Most 64 bit versions already do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top