summaryrefslogtreecommitdiff
path: root/src/utils/time.c
blob: b6bc960cc23c32d6266e0a317d6069f61c46b4aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "time.h"

#include <time.h>

int64_t nano_sleep(uint64_t nano_seconds) {
  struct timespec remaining,
      request = {nano_seconds / NANO_TO_SECOND, nano_seconds % NANO_TO_SECOND};
  nanosleep(&request, &remaining);
  int64_t ret = remaining.tv_sec * NANO_TO_SECOND + remaining.tv_nsec;
  if (ret < 0 || ret > NANO_TO_SECOND) {  // TODO: fix later
    return 0;
  } else {
    return ret;
  }
}

int64_t nano_time() {
  struct timespec t = {0, 0};
  clock_gettime(CLOCK_MONOTONIC, &t);
  return t.tv_sec * NANO_TO_SECOND + t.tv_nsec;
}