5
/* compare two time_t values
7
/* #include <timecmp.h>
13
/* The timecmp() function return an integer greater than, equal to, or
14
/* less than 0, according as the time t1 is greater than, equal to, or
15
/* less than the time t2. The comparison is made in a manner that is
16
/* insensitive to clock wrap-around, provided the underlying times are
17
/* within half of the time interval between the smallest and largest
18
/* representable time values.
22
/* The Secure Mailer license must be distributed with this software.
25
/* IBM T.J. Watson Research
27
/* Yorktown Heights, NY 10598, USA
34
/* timecmp - wrap-safe time_t comparison */
36
int timecmp(time_t t1, time_t t2)
38
time_t delta = t1 - t2;
43
#define UNSIGNED(type) ( ((type)-1) > ((type)0) )
46
* With a constant switch value, the compiler will emit only the code for
47
* the correct case, so the signed/unsigned test happens at compile time.
49
switch (UNSIGNED(time_t) ? 0 : 1) {
51
return ((2 * delta > delta) ? 1 : -1);
53
return ((delta > (time_t) 0) ? 1 : -1);
61
* Bit banging!! There is no official constant that defines the INT_MAX
62
* equivalent of the off_t type. Wietse came up with the following macro
63
* that works as long as off_t is some two's complement number.
65
* Note, however, that C99 permits signed integer representations other than
69
#define __MAXINT__(T) ((T) (((((T) 1) << ((sizeof(T) * CHAR_BIT) - 1)) ^ ((T) -1))))
73
time_t now = time((time_t *) 0);
75
/* Test that it works for normal times */
76
assert(timecmp(now + 10, now) > 0);
77
assert(timecmp(now, now) == 0);
78
assert(timecmp(now - 10, now) < 0);
80
/* Test that it works at a boundary time */
84
now = __MAXINT__(time_t);
86
assert(timecmp(now + 10, now) > 0);
87
assert(timecmp(now, now) == 0);
88
assert(timecmp(now - 10, now) < 0);