skip to content
==> src/clock.c <==
1/* The clocks. */
2#include "mesh/top_clock.h"
3
4#include <math.h>
5#include <time.h>
6
7static bool virtual_on;
8static uint64_t virtual_ns;
9
10mt_instant mt_now(void) {
11    if (virtual_on) return virtual_ns;
12    struct timespec ts;
13    clock_gettime(CLOCK_MONOTONIC, &ts);
14    return (uint64_t)ts.tv_sec * 1000000000u + (uint64_t)ts.tv_nsec;
15}
16
17void mt_clock_virtual(bool on, uint64_t ns) {
18    virtual_on = on;
19    virtual_ns = ns;
20}
21
22mt_duration mt_from_secs_f32(float s) {
23    if (!(s > 0)) return 0;
24    double ns = rint((double)s * 1e9); /* the f32 times 1e9 is exact in long arithmetic up to ~2^53 */
25    return (mt_duration)ns;
26}