summaryrefslogtreecommitdiff
path: root/src/utils/time.c
blob: 979a2c832075a9089e17712521b2c3b04c664050 (plain)
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
#include "time.h"

#include <stdio.h>

#ifdef PRINT_STATISTICS
struct timespec time_diff(struct timespec end, struct timespec start) {
  struct timespec temp;
  if ((end.tv_nsec - start.tv_nsec) < 0) {
    temp.tv_sec = end.tv_sec - start.tv_sec - 1;
    temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
  } else {
    temp.tv_sec = end.tv_sec - start.tv_sec;
    temp.tv_nsec = end.tv_nsec - start.tv_nsec;
  }
  return temp;
}

struct timespec time_add(struct timespec left, struct timespec right) {
  struct timespec result;
  result.tv_nsec = left.tv_nsec + right.tv_nsec;
  result.tv_sec = (left.tv_sec + right.tv_sec) + result.tv_nsec / 1000000000;
  result.tv_nsec %= 1000000000;
  return result;
}

void time_print(struct timespec time) {
  printf("%02ld:%02ld.%06ldus", time.tv_sec / 60, time.tv_sec % 60,
         time.tv_nsec / 1000);
}
#endif