summaryrefslogtreecommitdiffhomepage
path: root/src/timer.cpp
blob: f663aaf03161c9f96d530989309749a7c34d8c35 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "timer.hpp"
#include "compat/assert.hpp"
#include <ctime>
#include <cstdio>
#include <chrono>
#include <mg/Time.h>

namespace floormat {

using std::chrono::duration_cast;
using std::chrono::duration;

using Clock = std::chrono::high_resolution_clock;
using SystemClock = std::chrono::system_clock;
using Nsecs = duration<uint64_t, std::nano>;
using Millis = duration<unsigned, std::milli>;

namespace {

template<typename T> struct array_size_;
template<typename T, size_t N> struct array_size_<T(&)[N]> : std::integral_constant<size_t, N> {};
template<typename T, size_t N> struct array_size_<std::array<T, N>> : std::integral_constant<size_t, N> {};
template<typename T> constexpr inline auto array_size = array_size_<T>::value;

uint64_t get_time() noexcept { return duration_cast<Nsecs>(Clock::now().time_since_epoch()).count(); }

const uint64_t Epoch = get_time();

} // namespace

Time Time::now() noexcept
{
    auto val = get_time();
    auto ret = val - Epoch;
    return {ret};
}

Ns Time::update(const Time& ts) & noexcept
{
    auto ret = ts - *this;
    stamp = ts.stamp;
    return ret;
}

uint64_t Time::init() noexcept { return get_time(); }
bool Time::operator==(const Time&) const noexcept = default;
std::strong_ordering Time::operator<=>(const Time&) const noexcept = default;

float Time::to_seconds(const Ns& ts) noexcept { return float{ts} * 1e-9f; }
float Time::to_milliseconds(const Ns& ts) noexcept { return float{ts} * 1e-6f; }

const char* format_datetime_to_string(char (&buf)[fm_DATETIME_BUF_SIZE])
{
    constexpr const char* fmt = "%a, %d %b %Y %H:%M:%S.";
    constexpr size_t fmtsize = std::size("Thu 01 Mon 197000 00:00:00.");
    static_assert(array_size<decltype(buf)> - fmtsize == 4);
    const auto t    = SystemClock::now();
    const auto ms   = duration_cast<Millis>(t.time_since_epoch()) % 1000;
    const auto time = SystemClock::to_time_t(t);
    const auto* tm  = std::localtime(&time);
    auto len = std::strftime(buf, std::size(buf), fmt, tm);
    fm_assert(len > 0 && len <= fmtsize);
    auto len2 = std::sprintf(buf + len, "%03u", unsigned{ms.count()});
    fm_assert(len2 > 0 && len + (size_t)len2 < std::size(buf));
    return buf;
}



} // namespace floormat