Line data Source code
1 : // SPDX-License-Identifier: BSD-3-Clause 2 : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp) 3 : /// @file 4 : #pragma once 5 : 6 : #include <cstdint> 7 : #include <functional> 8 : #include <string> 9 : 10 : namespace scipp::core { 11 : 12 : /// Time-point similar to std::chrono::time point but without compile-time unit. 13 : /// The unit is determined by the Variable the time_point is stored in. 14 : class time_point { 15 : public: 16 : time_point() = default; 17 1946 : explicit time_point(int64_t duration) noexcept : m_duration{duration} {} 18 : 19 2301 : [[nodiscard]] int64_t time_since_epoch() const noexcept { 20 2301 : return m_duration; 21 : }; 22 : 23 111 : friend time_point operator+(const time_point a, const int64_t b) { 24 111 : return time_point{a.time_since_epoch() + b}; 25 : } 26 0 : friend time_point operator+(const int64_t a, const time_point b) { 27 0 : return time_point{a + b.time_since_epoch()}; 28 : } 29 : 30 2 : friend time_point operator-(const time_point a, const int64_t b) { 31 2 : return time_point{a.time_since_epoch() - b}; 32 : } 33 184 : friend int64_t operator-(const time_point a, const time_point b) { 34 184 : return a.time_since_epoch() - b.time_since_epoch(); 35 : } 36 : 37 2 : time_point &operator+=(const int64_t duration) { 38 2 : m_duration += duration; 39 2 : return *this; 40 : }; 41 : 42 2 : time_point &operator-=(const int64_t duration) { 43 2 : m_duration -= duration; 44 2 : return *this; 45 : }; 46 : 47 620 : bool operator==(const time_point &time) const noexcept { 48 620 : return m_duration == time.time_since_epoch(); 49 : } 50 0 : bool operator!=(const time_point &time) const noexcept { 51 0 : return m_duration != time.time_since_epoch(); 52 : } 53 643 : bool operator<(const time_point &time) const noexcept { 54 643 : return m_duration < time.time_since_epoch(); 55 : } 56 1 : bool operator>(const time_point &time) const noexcept { 57 1 : return m_duration > time.time_since_epoch(); 58 : } 59 16 : bool operator<=(const time_point &time) const noexcept { 60 16 : return m_duration <= time.time_since_epoch(); 61 : } 62 221 : bool operator>=(const time_point &time) const noexcept { 63 221 : return m_duration >= time.time_since_epoch(); 64 : } 65 : 66 : private: 67 : int64_t m_duration; 68 : }; 69 : 70 : } // namespace scipp::core 71 : 72 : namespace std { 73 : template <> struct hash<scipp::core::time_point> { 74 0 : size_t operator()(const scipp::core::time_point &tp) const noexcept { 75 0 : const auto time = tp.time_since_epoch(); 76 0 : return std::hash<std::decay_t<decltype(time)>>{}(time); 77 : } 78 : }; 79 : } // namespace std