summaryrefslogtreecommitdiff
path: root/src/voltronic/time.cc
blob: 20b45cccdf29155fcc8f862be252c6c15ddc8201 (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
33
34
35
36
37
38
// SPDX-License-Identifier: BSD-3-Clause

#include <ctime>
#include <cstdint>
#include <sys/time.h>
#include "time.h"

namespace voltronic {

u64 timestamp() {
    u64 ms = 0;

#if defined(CLOCK_MONOTONIC)
    static bool monotonic_clock_error = false;
    if (!monotonic_clock_error) {
        struct timespec ts = {0};
        if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
            ms = static_cast<u64>(ts.tv_sec);
            ms *= 1000;
            ms += static_cast<u64>(ts.tv_nsec / 1000000);
            return ms;
        } else {
            monotonic_clock_error = true;
        }
    }
#endif

    struct timeval tv = {0};
    if (gettimeofday(&tv, nullptr) == 0) {
        ms = static_cast<u64>(tv.tv_sec);
        ms *= 1000;
        ms += static_cast<u64>(tv.tv_usec / 1000);
    }

    return ms;
}

}