summaryrefslogtreecommitdiffhomepage
path: root/src/timer.hpp
blob: a05cc507e93d96dc873abca9a41e3ccb869d04a2 (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
#pragma once
#include <compare>

namespace floormat {

struct Ns
{
    explicit constexpr Ns(): stamp{0} {}
    explicit constexpr Ns(uint64_t x) : stamp{x} {}
    static Ns from_millis(uint64_t x);

    explicit operator uint64_t() const;
    explicit operator float() const;
    uint64_t operator*() const;

    friend Ns operator+(const Ns& lhs, const Ns& rhs);
    friend Ns operator-(const Ns& lhs, const Ns& rhs);
    friend Ns operator*(const Ns& lhs, uint64_t rhs);
    friend Ns operator*(uint64_t lhs, const Ns& rhs);
    friend Ns operator*(const Ns& lhs, float rhs);
    friend Ns operator*(float lhs, const Ns& rhs);

    friend uint64_t operator/(const Ns& lhs, const Ns& rhs);
    friend Ns operator/(const Ns& lhs, uint64_t rhs);
    friend uint64_t operator%(const Ns& lhs, const Ns& rhs);
    friend Ns operator%(const Ns& lhs, uint64_t rhs);

    friend bool operator==(const Ns& lhs, const Ns& rhs);
    friend std::strong_ordering operator<=>(const Ns& lhs, const Ns& rhs);
    friend Debug& operator<<(Debug& dbg, const Ns& box);

    uint64_t stamp;

    static constexpr uint64_t Min = 0, Max = (uint64_t)-1;
};

constexpr inline Ns Second{1000000000}, Millisecond{1000000};

struct Time final
{
    static Time now() noexcept;
    bool operator==(const Time&) const noexcept;
    std::strong_ordering operator<=>(const Time&) const noexcept;
    friend Ns operator-(const Time& lhs, const Time& rhs) noexcept;
    [[nodiscard]] Ns update(const Time& ts = now()) & noexcept;

    static float to_seconds(const Ns& ts) noexcept;
    static float to_milliseconds(const Ns& ts) noexcept;

    uint64_t stamp = init();

private:
    static uint64_t init() noexcept;
};

constexpr inline size_t fm_DATETIME_BUF_SIZE = 32;
const char* format_datetime_to_string(char(&buf)[fm_DATETIME_BUF_SIZE]);

} // namespace floormat