time.c 735 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#include <time.h>
#include <stdint.h>
#include "time.h"

long long timedelta(struct timespec *t1, struct timespec *t2) {
    long long delta = t1->tv_sec - t2->tv_sec;
    delta *= 1000000000;
    delta += t1->tv_nsec - t2->tv_nsec;
    return delta;
}

Andreas Schmidt's avatar
Andreas Schmidt committed
12 13 14 15 16 17
struct timespec abstime_now() {
    struct timespec now;
    clock_gettime(CLOCK_REALTIME, &now);
    return now;
}

rna's avatar
rna committed
18 19 20 21 22 23 24 25
// < 0: a less than b (b is in the future)
// > 0: a greater b (b is in the past)
// == 0: a equal b
int64_t PrrtTimestamp_cmp(prrtTimestamp_t a, prrtTimestamp_t b) {
    prrtTimestamp_t diff = (prrtTimestamp_t) (a - b);
    if (diff < TIMESTAMP_SPACE / 2) {
        return diff;
    } else {
26
        return ((int64_t)diff) - ((int64_t)(TIMESTAMP_SPACE));
rna's avatar
rna committed
27
    }
Andreas Schmidt's avatar
Andreas Schmidt committed
28
}