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

namespace floormat {

struct Ns
{
    static constexpr uint64_t Min = 0, Max = (uint64_t)-1;
    static constexpr uint64_t Second = 1000000000, Millisecond = 1000000;

    uint64_t stamp;

    explicit constexpr Ns(): stamp{0} {}
    explicit constexpr Ns(uint64_t x) : stamp{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 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);
};

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& a, const Time& b) 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