/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #pragma once #include namespace OpenVulkano::Math { class Timestamp final { static constexpr uint64_t SECONDS_TO_NANOS = 1'000'000'000ull; static constexpr double NANOS_TO_SECONDS = 1.0 / SECONDS_TO_NANOS; uint64_t timestamp; public: explicit constexpr Timestamp(uint64_t nanos = 0) : timestamp(nanos) {} explicit constexpr Timestamp(double seconds) : timestamp(static_cast(seconds * SECONDS_TO_NANOS)) {} [[nodiscard]] constexpr uint64_t GetNanos() const { return timestamp; } [[nodiscard]] constexpr double GetSeconds() const { return timestamp * NANOS_TO_SECONDS; } constexpr Timestamp& operator+=(const Timestamp& rhs) { timestamp += rhs.timestamp; return *this; } constexpr Timestamp& operator-=(const Timestamp& rhs) { timestamp -= rhs.timestamp; return *this; } constexpr Timestamp& operator =(double time) { timestamp = Timestamp(time).timestamp; return *this; } constexpr Timestamp& operator =(uint64_t time) { timestamp = time; return *this; } constexpr inline bool operator==(const Timestamp& rhs) const { return GetNanos() == rhs.GetNanos(); } constexpr inline bool operator!=(const Timestamp& rhs) const { return !(*this == rhs); } constexpr inline bool operator<(const Timestamp& rhs) const { return GetNanos() < rhs.GetNanos(); } constexpr inline bool operator>(const Timestamp& rhs) const { return rhs < *this; } constexpr inline bool operator>=(const Timestamp& rhs) const { return !(*this < rhs); } constexpr inline bool operator<=(const Timestamp& rhs) const { return !(rhs < *this); } constexpr inline Timestamp operator-(const Timestamp& rhs) const { return Timestamp(GetNanos() - rhs.GetNanos()); } constexpr inline Timestamp operator+(const Timestamp& rhs) const { return Timestamp(GetNanos() + rhs.GetNanos()); } constexpr inline double operator/(const Timestamp& rhs) const { return GetNanos() / static_cast(rhs.GetNanos()); } }; }