Wayland compositor (wlroots)
git clone https://git.lucas.co/cce-compositor.git
scenefx/util/time.c (797B)
1 #include <stdint.h>
2 #include <time.h>
3
4 #include "util/time.h"
5
6 int64_t timespec_to_msec(const struct timespec *a) {
7 return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000;
8 }
9
10 int64_t timespec_to_nsec(const struct timespec *a) {
11 return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec;
12 }
13
14 void timespec_from_nsec(struct timespec *r, int64_t nsec) {
15 r->tv_sec = nsec / NSEC_PER_SEC;
16 r->tv_nsec = nsec % NSEC_PER_SEC;
17 }
18
19 int64_t get_current_time_msec(void) {
20 struct timespec now;
21 clock_gettime(CLOCK_MONOTONIC, &now);
22 return timespec_to_msec(&now);
23 }
24
25 void timespec_sub(struct timespec *r, const struct timespec *a,
26 const struct timespec *b) {
27 r->tv_sec = a->tv_sec - b->tv_sec;
28 r->tv_nsec = a->tv_nsec - b->tv_nsec;
29 if (r->tv_nsec < 0) {
30 r->tv_sec--;
31 r->tv_nsec += NSEC_PER_SEC;
32 }
33 }