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

namespace floormat {

struct Ns;

struct Ns
{
    friend Ns operator+(const Ns& lhs, const Ns& rhs);
    friend Ns operator-(const Ns& lhs, const Ns& rhs);
    friend uint64_t operator/(const Ns& lhs, const Ns& rhs);
    friend Ns operator%(const Ns& lhs, const Ns& rhs);

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

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

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

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

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