timestamp.h
Last updated: 12-Sep-2025
Go to the documentation of this file.
1Â // -*- Mode: C++; -*- 2Â #ifndef _SDI_TIMESTAMP_H_ 3Â #define _SDI_TIMESTAMP_H_ 4Â 7Â #include <time.h> 8Â 9Â namespace sdi 10Â { 11Â #if 0 12Â } 13Â #endif 14Â 16Â class TimeStamp: public timespec 17Â { 18Â public: 20Â static TimeStamp Clock(); 21Â 23Â static TimeStamp Monotonic(); 24Â 26Â class Unit_MS {}; 27Â 29Â class Unit_S {}; 30Â 32Â static Unit_MS MS; 33Â 35Â static Unit_S S; 36Â 38Â TimeStamp(time_t sec, long nsec); 39Â 41Â TimeStamp(char sec) 42Â { 43Â tv_sec = sec; 44Â tv_nsec = 0; 45Â } 46Â 48Â TimeStamp(unsigned char sec) 49Â { 50Â tv_sec = sec; 51Â tv_nsec = 0; 52Â } 53Â 55Â TimeStamp(short sec) 56Â { 57Â tv_sec = sec; 58Â tv_nsec = 0; 59Â } 60Â 62Â TimeStamp(unsigned short sec) 63Â { 64Â tv_sec = sec; 65Â tv_nsec = 0; 66Â } 67Â 69Â TimeStamp(int sec) 70Â { 71Â tv_sec = sec; 72Â tv_nsec = 0; 73Â } 74Â 76Â TimeStamp(unsigned sec) 77Â { 78Â tv_sec = sec; 79Â tv_nsec = 0; 80Â } 81Â 83Â TimeStamp(long sec = 0) 84Â { 85Â tv_sec = sec; 86Â tv_nsec = 0; 87Â } 88Â 90Â TimeStamp(unsigned long sec) 91Â { 92Â tv_sec = sec; 93Â tv_nsec = 0; 94Â } 95Â 97Â TimeStamp(float t); 98Â 100Â TimeStamp(double t); 101Â 103Â TimeStamp &operator=(time_t sec) 104Â { 105Â tv_sec = sec; 106Â tv_nsec = 0; 107Â return *this; 108Â } 109Â 111Â TimeStamp &operator=(double t); 112Â 113Â TimeStamp &operator+=(const TimeStamp &o); 114Â TimeStamp &operator-=(const TimeStamp &o); 115Â 117Â double get() const 118Â { 119Â return tv_sec + tv_nsec / 1000000000.0; 120Â } 121Â 123Â void set(double t) 124Â { 125Â operator=(t); 126Â } 127Â 129Â time_t s() const 130Â { 131Â return tv_sec; 132Â } 133Â 135Â long ms() const 136Â { 137Â return tv_sec * 1000 + tv_nsec / 1000000; 138Â } 139Â 141Â void normalize(); 142Â 144Â void clear() 145Â { 146Â tv_sec = 0; 147Â tv_nsec = 0; 148Â } 149Â }; 150Â 151Â 153Â TimeStamp operator+(const TimeStamp &a, const TimeStamp &b); 154Â 156Â TimeStamp operator-(const TimeStamp &a, const TimeStamp &b); 157Â 159Â inline bool operator==(const TimeStamp &a, const TimeStamp &b) 160Â { 161Â return a.tv_sec == b.tv_sec && a.tv_nsec == b.tv_nsec; 162Â } 163Â 165Â inline bool operator!=(const TimeStamp &a, const TimeStamp &b) 166Â { 167Â return a.tv_sec != b.tv_sec && a.tv_nsec != b.tv_nsec; 168Â } 169Â 171Â inline bool operator<(const TimeStamp &a, const TimeStamp &b) 172Â { 173Â return a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec); 174Â } 175Â 177Â inline bool operator<=(const TimeStamp &a, const TimeStamp &b) 178Â { 179Â return a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec <= b.tv_nsec); 180Â } 181Â 183Â inline bool operator>(const TimeStamp &a, const TimeStamp &b) 184Â { 185Â return a.tv_sec > b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec > b.tv_nsec); 186Â } 187Â 189Â inline bool operator>=(const TimeStamp &a, const TimeStamp &b) 190Â { 191Â return a.tv_sec > b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec >= b.tv_nsec); 192Â } 193Â 195Â inline TimeStamp operator*(int t, TimeStamp::Unit_MS) 196Â { 197Â return TimeStamp(t / 1000, t % 1000 * 1000000); 198Â } 199Â 201Â inline TimeStamp operator*(int t, TimeStamp::Unit_S) 202Â { 203Â return TimeStamp(t, 0); 204Â } 205Â 206Â 207Â } // namespace 208Â 209Â #endif
Rate this article: