Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cTimeSys.h
Go to the documentation of this file.
1 //
5 //
6 
7 #ifndef _INC_cTimeSys_H
8 #define _INC_cTimeSys_H
9 #ifndef NO_PRAGMA_ONCE
10 #pragma once
11 #endif
12 
13 #include "cUnitTestDecl.h"
14 #include "cDebugAssert.h"
15 #include <time.h> // timespec
16 
17 namespace Gray
18 {
19  typedef int TIMESECD_t;
20  typedef float TIMESECF_t;
21 
23 #if 0 // def USE_64BIT
24  typedef UINT64 TIMESYS_t;
25  typedef INT64 TIMESYSD_t;
26 #else
27  typedef UINT32 TIMESYS_t;
28  typedef INT32 TIMESYSD_t;
29 #endif
30 
31 #if defined(__linux__)
32  class cTimeSpec : public /* struct*/ timespec
33  {
39 
40  public:
41  static const UINT k_FREQ = 1000000000; // billionths of a sec.
42 
43  cTimeSpec() noexcept
44  {
45  // undefined. may use clock_gettime() on it.
46  }
47  cTimeSpec(TIMESYSD_t nMilliSeconds) noexcept
48  {
49  put_mSec(nMilliSeconds);
50  }
51  cTimeSpec(TIMESECD_t iSeconds, int iNanoSec) noexcept
52  {
53  this->tv_sec = iSeconds;
54  this->tv_nsec = iNanoSec; // nano = billionths of a sec.
55  }
56  void put_mSec(TIMESYSD_t nMilliSeconds) noexcept
57  {
58  // milliSeconds.
59  this->tv_sec = nMilliSeconds / 1000;
60  this->tv_nsec = (nMilliSeconds % 1000) * 1000000; // mSec to nano = billionths of a sec.
61  }
62  TIMESYS_t get_mSec() const noexcept
63  {
65  TIMESYS_t nTicks = this->tv_sec * 1000;
66  nTicks += this->tv_nsec / 1000000; // to mSec from nSec
67  return nTicks;
68  }
69  UINT64 get_nSec() const noexcept
70  {
72  return (((UINT64)this->tv_sec) * cTimeSpec::k_FREQ) + this->tv_nsec;
73  }
74  void InitTimeNow() noexcept
75  {
79  ::clock_gettime(CLOCK_MONOTONIC, this);
80  }
81  void InitTimeNow1() noexcept
82  {
85  ::clock_gettime(CLOCK_REALTIME, this);
86  }
87  };
88 #endif
89 
90  //****************************************************************************
91 
93  {
98  public:
99  static const TIMESYS_t k_CLEAR = 0;
100  static const TIMESYS_t k_FREQ = 1000;
101  static const TIMESYS_t k_INF = UINT_MAX;
102  static const TIMESYSD_t k_DMAX = INT_MAX;
103 
104  private:
105  TIMESYS_t m_TimeSys;
106 
107  public:
108  cTimeSys() noexcept
109  : m_TimeSys(k_CLEAR)
110  {
111  }
112  cTimeSys(const cTimeSys& t) noexcept
113  : m_TimeSys(t.m_TimeSys)
114  {
115  }
116  cTimeSys(TIMESYS_t t) noexcept
117  : m_TimeSys(t)
118  {
119  }
120 
121  static TIMESYS_t inline GetTimeNow() noexcept
122  {
125 #ifdef _WIN32
126  // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724408(v=vs.85).aspx
127 #if 0 // def USE_64BIT
128  return ::GetTickCount64(); // why not use 64 bits ???
129 #else
130  return ::GetTickCount();
131 #endif
132 #elif defined(__linux__)
133  cTimeSpec tNow;
134  tNow.InitTimeNow();
135  return tNow.get_mSec();
136 #else
137 #error NOOS
138 #endif
139  }
140 
141  static unsigned long GRAYCALL WaitSpin(TIMESYSD_t t);
142 
143  bool isTimeValid() const noexcept
144  {
145  return m_TimeSys > k_CLEAR;
146  }
147  TIMESYS_t get_TimeSys() const noexcept
148  {
149  return m_TimeSys;
150  }
151 
152  void InitTime(TIMESYS_t t = k_CLEAR) noexcept
153  {
154  m_TimeSys = t;
155  }
156  void InitTimeNow() noexcept
157  {
158  m_TimeSys = GetTimeNow();
159  }
160  void InitTimeNowPlusSys(TIMESYSD_t iOffset) noexcept
161  {
162  m_TimeSys = GetTimeNow() + iOffset;
163  }
164  void InitTimeNowPlusSec(float fOffsetSec) noexcept
165  {
166  InitTimeNowPlusSys((TIMESYSD_t)(fOffsetSec * k_FREQ));
167  }
168  bool isTimeFuture() const noexcept
169  {
170  return m_TimeSys > GetTimeNow(); // GetTimeNow
171  }
172 
173  TIMESYSD_t get_TimeTilSys() const noexcept
174  {
177  if (m_TimeSys == 0)
178  return -k_DMAX;
179  if (m_TimeSys == k_INF)
180  return k_DMAX;
181  return (TIMESYSD_t)(m_TimeSys - GetTimeNow());
182  }
183  TIMESYSD_t get_AgeSys() const noexcept
184  {
189  if (m_TimeSys == 0)
190  return k_DMAX;
191  if (m_TimeSys == k_INF)
192  return -k_DMAX;
193  return (TIMESYSD_t)(GetTimeNow() - m_TimeSys);
194  }
195 
196  TIMESECF_t get_TimeTilSecF() const noexcept
197  {
199  return get_TimeTilSys() / (TIMESECF_t)k_FREQ;
200  }
201  TIMESECF_t get_AgeSecF() const noexcept
202  {
204  return get_AgeSys() / (TIMESECF_t)k_FREQ;
205  }
206  TIMESECD_t get_AgeSec() const noexcept
207  {
210  return get_AgeSys() / k_FREQ;
211  }
212 
214  };
215 
216  //****************************************************************************
217 
218 #ifdef _WIN32
219  typedef LONGLONG TIMEPERF_t;
220 #else
221  typedef UINT64 TIMEPERF_t;
222 #endif
223 
225  {
229 
230  public:
232 #ifdef _WIN32
233  static TIMEPERF_t sm_nFreq;
234 #else // __linux__
235  static const TIMEPERF_t sm_nFreq = cTimeSpec::k_FREQ;
236 #endif
237 
238  public:
239  cTimePerf(TIMEPERF_t nTime = 0) noexcept
240  : m_nTime(nTime)
241  {
243  }
244  cTimePerf(int nTime) noexcept
245  : m_nTime(nTime)
246  {
248  }
249  cTimePerf(bool bTrue) noexcept
250  {
251  // Indicate I want the current time.
252  if (bTrue)
253  InitTimeNow();
254  else
255  m_nTime = 0; // The test is turned off. don't record time.
256  }
257 
258  bool isTimeValid() const noexcept
259  {
260  return m_nTime != 0;
261  }
262 
263  static bool GRAYCALL InitFreq() noexcept;
264  void InitTimeNow() noexcept;
265 
266  TIMEPERF_t get_Perf() const noexcept
267  {
269  return m_nTime;
270  }
271  TIMEPERF_t GetAgeDiff(cTimePerf tStop) const noexcept
272  {
274  return tStop.m_nTime - this->m_nTime;
275  }
276  TIMEPERF_t get_AgePerf() const noexcept
277  {
279  cTimePerf tStop(true);
280  return GetAgeDiff(tStop);
281  }
282 
283  static inline double GRAYCALL ToSeconds(TIMEPERF_t t) noexcept
284  {
285  return ((double)t) / ((double)sm_nFreq);
286  }
287  double get_Seconds() const noexcept
288  {
290  // ASSERT( k_nFreq != 0 );
291  return ToSeconds(m_nTime);
292  }
293  double get_AgeSeconds() const noexcept
294  {
296  const TIMEPERF_t tDiff = get_AgePerf();
297  // ASSERT( k_nFreq != 0 );
298  return ToSeconds(tDiff);
299  }
300 
301  static double GRAYCALL ToDays(TIMEPERF_t t) noexcept;
302  double get_Days() const noexcept
303  {
306  return ToDays(m_nTime);
307  }
308  };
309 }
310 #endif // _INC_cTimeSys_H
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
Definition: cTimeSys.h:225
double get_Seconds() const noexcept
Definition: cTimeSys.h:287
cTimePerf(int nTime) noexcept
Definition: cTimeSys.h:244
cTimePerf(TIMEPERF_t nTime=0) noexcept
Definition: cTimeSys.h:239
TIMEPERF_t get_AgePerf() const noexcept
Definition: cTimeSys.h:276
cTimePerf(bool bTrue) noexcept
Definition: cTimeSys.h:249
TIMEPERF_t m_nTime
Arbitrary start time in k_nFreq units. 64 byte unsigned type.
Definition: cTimeSys.h:231
TIMEPERF_t GetAgeDiff(cTimePerf tStop) const noexcept
Definition: cTimeSys.h:271
bool isTimeValid() const noexcept
Definition: cTimeSys.h:258
double get_AgeSeconds() const noexcept
Definition: cTimeSys.h:293
static double __stdcall ToSeconds(TIMEPERF_t t) noexcept
Definition: cTimeSys.h:283
double get_Days() const noexcept
Definition: cTimeSys.h:302
Definition: cTimeSys.h:93
TIMESECD_t get_AgeSec() const noexcept
Definition: cTimeSys.h:206
cTimeSys(TIMESYS_t t) noexcept
Definition: cTimeSys.h:116
void InitTime(TIMESYS_t t=k_CLEAR) noexcept
Definition: cTimeSys.h:152
void InitTimeNow() noexcept
Definition: cTimeSys.h:156
TIMESECF_t get_TimeTilSecF() const noexcept
Definition: cTimeSys.h:196
bool isTimeFuture() const noexcept
Definition: cTimeSys.h:168
cTimeSys() noexcept
Definition: cTimeSys.h:108
TIMESYSD_t get_TimeTilSys() const noexcept
Definition: cTimeSys.h:173
TIMESYS_t get_TimeSys() const noexcept
Definition: cTimeSys.h:147
static TIMESYS_t GetTimeNow() noexcept
Definition: cTimeSys.h:121
void InitTimeNowPlusSec(float fOffsetSec) noexcept
Definition: cTimeSys.h:164
void InitTimeNowPlusSys(TIMESYSD_t iOffset) noexcept
Definition: cTimeSys.h:160
bool isTimeValid() const noexcept
Definition: cTimeSys.h:143
TIMESECF_t get_AgeSecF() const noexcept
Definition: cTimeSys.h:201
TIMESYSD_t get_AgeSys() const noexcept
Definition: cTimeSys.h:183
cTimeSys(const cTimeSys &t) noexcept
Definition: cTimeSys.h:112
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
INT32 TIMESYSD_t
Time delta. signed milli-Seconds Span. cTimeSys::k_DMAX, cTimeSys::k_INF = MAILSLOT_WAIT_FOREVER.
Definition: cTimeSys.h:28
int TIMESECD_t
signed delta seconds. like TIMESEC_t. redefined in TimeUnits.h.
Definition: cTimeSys.h:19
UINT64 TIMEPERF_t
The system very high precision performance timer. cTimeSpec.
Definition: cTimeSys.h:221
float TIMESECF_t
delta float seconds.
Definition: cTimeSys.h:20
UINT32 TIMESYS_t
TIMESYS_t = The normal system tick timer. milli-seconds since start of system/app ?
Definition: cTimeSys.h:27