/Users/alexjokela/projects/lattice/src/time_ops.c
Line | Count | Source |
1 | | #include "time_ops.h" |
2 | | #include <time.h> |
3 | | #include <stdlib.h> |
4 | | #include <string.h> |
5 | | #include <stdio.h> |
6 | | #include <errno.h> |
7 | | |
8 | 51 | int64_t time_now_ms(void) { |
9 | 51 | struct timespec ts; |
10 | 51 | clock_gettime(CLOCK_REALTIME, &ts); |
11 | 51 | return (int64_t)ts.tv_sec * 1000 + (int64_t)(ts.tv_nsec / 1000000); |
12 | 51 | } |
13 | | |
14 | 3 | bool time_sleep_ms(int64_t ms, char **err) { |
15 | 3 | if (ms < 0) { |
16 | 0 | *err = strdup("sleep() expects non-negative milliseconds"); |
17 | 0 | return false; |
18 | 0 | } |
19 | 3 | struct timespec ts; |
20 | 3 | ts.tv_sec = (time_t)(ms / 1000); |
21 | 3 | ts.tv_nsec = (long)((ms % 1000) * 1000000); |
22 | 3 | if (nanosleep(&ts, NULL) != 0 && errno != EINTR) { |
23 | 0 | *err = strdup("sleep: nanosleep failed"); |
24 | 0 | return false; |
25 | 0 | } |
26 | 3 | return true; |
27 | 3 | } |