amiga-tz/library/time_increment.c

36 lines
937 B
C

#include "time_header.h"
bool increment_overflow(int *const ip, int j)
{
int const i = *ip;
/*
** If i >= 0 there can only be overflow if i + j > INT_MAX
** or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow.
** If i < 0 there can only be overflow if i + j < INT_MIN
** or if j < INT_MIN - i; given i < 0, INT_MIN - i cannot overflow.
*/
if ((i >= 0) ? (j > INT_MAX - i) : (j < INT_MIN - i)) {
return true;
}
*ip += j;
return false;
}
bool increment_overflow_time(time_t *tp, int_fast32_t j)
{
/*
** This is like
** 'if (! (time_t_min <= *tp + j && *tp + j <= time_t_max)) ...',
** except that it does the right thing even if *tp + j would overflow.
*/
if (! (j < 0
? (TYPE_SIGNED(time_t) ? time_t_min - j <= *tp : -1 - j < *tp)
: *tp <= time_t_max - j))
return true;
*tp += j;
return false;
}