timestamp.h
Last updated: 17-Sep-2025
Go to the documentation of this file.
1Â // -*- Mode: C++; -*- 2Â #ifndef TIMESTAMP_H_20141009 3Â #define TIMESTAMP_H_20141009 4Â 7Â #include <time.h> 8Â 9Â #if defined _WIN32 && defined VFI_GUIPRT_SHARED_EXPORT 10Â # define DllSpec __declspec(dllexport) 11Â #elif defined __GNUC__ && defined VFI_GUIPRT_SHARED_EXPORT 12Â # define DllSpec __attribute__((visibility ("default"))) 13Â #else 14Â # define DllSpec 15Â #endif 16Â 17Â #if defined(_WIN32) && !defined(_TIMESPEC_DEFINED) 18Â #define _TIMESPEC_DEFINED 19Â struct timespec { 20Â time_t tv_sec; 21Â long tv_nsec; 22Â }; 23Â #endif /* _TIMESPEC_DEFINED */ 24Â 25Â 26Â namespace vfihtml { 27Â #if 0 28Â } 29Â #endif 30Â 32Â class DllSpec TimeStamp: public timespec 33Â { 34Â public: 36Â static TimeStamp Clock(); 37Â 39Â static TimeStamp Monotonic(); 40Â 42Â class Unit_MS {}; 43Â 45Â class Unit_S {}; 46Â 48Â static Unit_MS MS; 49Â 51Â static Unit_S S; 52Â 54Â TimeStamp(time_t sec, long nsec); 55Â 57Â TimeStamp(char sec) { tv_sec=sec; tv_nsec=0; } 58Â 60Â TimeStamp(unsigned char sec) { tv_sec=sec; tv_nsec=0; } 61Â 63Â TimeStamp(short sec) { tv_sec=sec; tv_nsec=0; } 64Â 66Â TimeStamp(unsigned short sec) { tv_sec=sec; tv_nsec=0; } 67Â 69Â TimeStamp(int sec) { tv_sec=sec; tv_nsec=0; } 70Â 72Â TimeStamp(unsigned sec) { tv_sec=sec; tv_nsec=0; } 73Â 75Â TimeStamp(long sec=0) { tv_sec=sec; tv_nsec=0; } 76Â 78Â TimeStamp(unsigned long sec) { tv_sec=sec; tv_nsec=0; } 79Â 81Â TimeStamp(float t); 82Â 84Â TimeStamp(double t); 85Â 87Â TimeStamp &operator=(time_t sec) { tv_sec=sec; tv_nsec=0; return *this;} 88Â 90Â TimeStamp &operator=(double t); 91Â 92Â TimeStamp &operator+=(const TimeStamp &o); 93Â TimeStamp &operator-=(const TimeStamp &o); 94Â 96Â double get() const { return tv_sec+tv_nsec/1000000000.0; } 97Â 99Â void set(double t) { operator=(t); } 100Â 102Â time_t s() const { return tv_sec; } 103Â 105Â long ms() const { return tv_sec*1000+tv_nsec/1000000; } 106Â 108Â void normalize(); 109Â 111Â void clear() { tv_sec=0; tv_nsec=0; } 112Â }; 113Â 114Â 116Â DllSpec TimeStamp operator+(const TimeStamp &a, const TimeStamp &b); 117Â 119Â DllSpec TimeStamp operator-(const TimeStamp &a, const TimeStamp &b); 120Â 122Â inline bool operator==(const TimeStamp &a, const TimeStamp &b) { return a.tv_sec==b.tv_sec && a.tv_nsec==b.tv_nsec; } 123Â 125Â inline bool operator!=(const TimeStamp &a, const TimeStamp &b) { return a.tv_sec!=b.tv_sec && a.tv_nsec!=b.tv_nsec; } 126Â 128Â inline bool operator<(const TimeStamp &a, const TimeStamp &b) { return a.tv_sec<b.tv_sec || (a.tv_sec==b.tv_sec && a.tv_nsec<b.tv_nsec); } 129Â 131Â inline bool operator<=(const TimeStamp &a, const TimeStamp &b) { return a.tv_sec<b.tv_sec || (a.tv_sec==b.tv_sec && a.tv_nsec<=b.tv_nsec); } 132Â 134Â inline bool operator>(const TimeStamp &a, const TimeStamp &b) { return a.tv_sec>b.tv_sec || (a.tv_sec==b.tv_sec && a.tv_nsec>b.tv_nsec); } 135Â 137Â inline bool operator>=(const TimeStamp &a, const TimeStamp &b) { return a.tv_sec>b.tv_sec || (a.tv_sec==b.tv_sec && a.tv_nsec>=b.tv_nsec); } 138Â 140Â inline TimeStamp operator*(int t, TimeStamp::Unit_MS) { 141Â return TimeStamp(t/1000,t%1000*1000000); 142Â } 143Â 145Â inline TimeStamp operator*(int t, TimeStamp::Unit_S) { 146Â return TimeStamp(t,0); 147Â } 148Â 149Â 150Â } // namespace 151Â 152Â #undef DllSpec 153Â 154Â #endif
Rate this article: