time-util: rename usec_sub() to usec_sub_signed() and add usec_sub_unsigned()

Quite often we just want to subtract two normal usec_t values, hence
provide an implementation for that.
This commit is contained in:
Lennart Poettering
2017-06-22 20:51:06 +02:00
parent 8825742c44
commit 54d8ef14d8
4 changed files with 49 additions and 23 deletions
+9 -5
View File
@@ -169,19 +169,23 @@ static inline usec_t usec_add(usec_t a, usec_t b) {
return c;
}
static inline usec_t usec_sub(usec_t timestamp, int64_t delta) {
if (delta < 0)
return usec_add(timestamp, (usec_t) (-delta));
static inline usec_t usec_sub_unsigned(usec_t timestamp, usec_t delta) {
if (timestamp == USEC_INFINITY) /* Make sure infinity doesn't degrade */
return USEC_INFINITY;
if (timestamp < (usec_t) delta)
if (timestamp < delta)
return 0;
return timestamp - delta;
}
static inline usec_t usec_sub_signed(usec_t timestamp, int64_t delta) {
if (delta < 0)
return usec_add(timestamp, (usec_t) (-delta));
else
return usec_sub_unsigned(timestamp, (usec_t) delta);
}
#if SIZEOF_TIME_T == 8
/* The last second we can format is 31. Dec 9999, 1s before midnight, because otherwise we'd enter 5 digit year
* territory. However, since we want to stay away from this in all timezones we take one day off. */