aboutsummaryrefslogtreecommitdiff
path: root/source/time.c
blob: e2c47b0840629fa469f9abacdb5545a5796b72aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "time.h"

struct timespec timespec_add(struct timespec a, struct timespec b) {
    a.tv_sec += b.tv_sec;
    a.tv_nsec += b.tv_nsec;
    if (a.tv_nsec >= 1000000000) {
        a.tv_sec++;
        a.tv_nsec -= 1000000000;
    }
    return a;
}

struct timespec timespec_sub(struct timespec a, struct timespec b) {
    a.tv_sec -= b.tv_sec;
    a.tv_nsec -= b.tv_nsec;
    if (a.tv_nsec < 0) {
        a.tv_sec--;
        a.tv_nsec += 1000000000;
    }
    return a;
}

int timespec_cmp(struct timespec a, struct timespec b) {
  return a.tv_sec > b.tv_sec ?
    (1) :
    a.tv_sec < b.tv_sec ?
    (-1) :
    (
      a.tv_nsec > b.tv_nsec ?
      (1) :
      a.tv_nsec < b.tv_nsec ?
      (-1) : 0
    );
}

struct timespec timespec_max(struct timespec a, struct timespec b) {
  return a.tv_sec > b.tv_sec ?
    a :
    a.tv_sec < b.tv_sec ?
    b :
    (
      a.tv_nsec > b.tv_nsec ?
      a : b
    );
}

struct timespec timespec_min(struct timespec a, struct timespec b) {
  return a.tv_sec < b.tv_sec ?
    a :
    a.tv_sec > b.tv_sec ?
    b :
    (
      a.tv_nsec < b.tv_nsec ?
      a : b
    );
}