ddc
ddcTime.h
Go to the documentation of this file.
1 //-*- Mode: C++ -*-
2 //
3 // DDC originally by Alexey Sokirko
4 // Changes and modifications 2011-2018 by Bryan Jurish
5 //
6 // This file is part of DDC
7 //
8 // DDC is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // DDC is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with DDC. If not, see <http://www.gnu.org/licenses/>.
20 //
21 #ifndef DDC_TIME_H
22 #define DDC_TIME_H
23 
24 #include "../CommonLib/ddcConfig.h"
25 #include <time.h>
26 #include <string>
27 #include <map>
28 #include <vector>
29 
30 //======================================================================
31 // constants
32 
34 extern const size_t DDCDateBufferSize;
35 
37 extern const size_t DDCTimeBufferSize;
38 
39 //======================================================================
40 // DDCTime : 1-second resolution clock
42 {
43  //----------------------------------------------------------------------
44  // Data
46  time_t m_t;
47 
48  //----------------------------------------------------------------------
49  // constructors etc
50 
53  {};
54 
56  DDCTimeUnix(const time_t time_)
57  { Set(time_); };
58 
61  : m_t(X.m_t)
62  {};
63 
65  virtual ~DDCTimeUnix()
66  {};
67 
69  inline static DDCTimeUnix Now()
70  {
71  DDCTimeUnix t;
72  t.Set();
73  return t;
74  };
75 
77  inline void Set()
78  { time(&m_t); };
79 
81  inline void Set(const time_t time_)
82  { m_t = time_; };
83 
85  inline void Set(const DDCTimeUnix& t)
86  { Set(t.m_t); };
87 
88  //----------------------------------------------------------------------
89  // API
90 
92  inline time_t Time() const
93  { return m_t; };
94 
96  struct tm CalendarTime() const;
97 
103  virtual void DateTime(char* DateBuf, char* TimeBuf) const;
104 
106  inline std::string DateStr() const
107  {
108  char DateBuf[DDCDateBufferSize];
109  this->DateTime(DateBuf, NULL);
110  return std::string(DateBuf);
111  };
112 
114  inline std::string TimeStr() const
115  {
116  char TimeBuf[DDCTimeBufferSize];
117  this->DateTime(NULL,TimeBuf);
118  return std::string(TimeBuf);
119  };
120 
122  double Elapsed(const DDCTimeUnix& t0) const;
123 };
124 
125 //======================================================================
126 // DDCTimeHiRes : nanosecond-resolution clock
127 #ifdef HAVE_CLOCK_GETTIME
128 struct DDCTimeHiRes : public DDCTimeUnix
129 {
130  //----------------------------------------------------------------------
131  // Data
133  struct timespec m_ts;
134 
135  //----------------------------------------------------------------------
136  // constructors etc
137 
140  {};
141 
143  DDCTimeHiRes(const struct timespec &ts_)
144  { Set(ts_); };
145 
148  { Set(t.m_ts); };
149 
152  { Set(t.m_t); };
153 
155  virtual ~DDCTimeHiRes()
156  {};
157 
159  inline static DDCTimeHiRes Now()
160  {
161  DDCTimeHiRes t;
162  t.Set();
163  return t;
164  };
165 
167  inline void Set()
168  {
169  clock_gettime(CLOCK_REALTIME, &m_ts);
170  DDCTimeUnix::Set(m_ts.tv_sec);
171  };
172 
174  inline void Set(const time_t time_)
175  {
176  DDCTimeUnix::Set(time_);
177  m_ts.tv_sec = time_;
178  m_ts.tv_nsec = 0;
179  };
180 
182  inline void Set(const struct timespec &ts_)
183  {
184  m_t = ts_.tv_sec;
185  m_ts = ts_;
186  };
187 
189  inline void Set(const DDCTimeUnix& t)
190  { Set(t.m_t); };
191 
193  inline void Set(const DDCTimeHiRes& t)
194  { *this = t; };
195 
196  //----------------------------------------------------------------------
197  // API
198 
199  /* inherited: get low-resultion time_t time */
200  //time_t Time() const;
201 
202  /* inherited: get (low-resolution) calendar time; adapted from old RmlGetCurrentTime() */
203  //struct tm CalendarTime() const;
204 
210  virtual void DateTime(char* DateBuf, char* TimeBuf) const;
211 
212  /* inherited: get date string in ISO-8601 format (YYYY-MM-DD) */
213  //inline std::string DateStr() const
214 
216  // inline std::string TimeStr() const
217 
219  double Elapsed(const DDCTimeHiRes& t0) const;
220 };
221 
222 #else /* !defined(HAVE_CLOCK_GETTIME) */
223 
225 typedef struct DDCTimeUnix DDCTimeHiRes;
226 
227 #endif /* defined(HAVE_CLOCK_GETTIME) */
228 
229 #ifdef DDC_USE_HIRES_CLOCK
230 
231 typedef struct DDCTimeHiRes DDCTime;
232 #else
233 
234 typedef struct DDCTimeUnix DDCTime;
235 #endif
236 
237 //======================================================================
238 // DDCTimeAvg : exponential moving average
240 {
241  //----------------------------------------------------------------------
242  // typedefs
243 
245  typedef size_t DelayT;
246 
248  typedef double ValueT;
249 
251  typedef DDCTimeHiRes TimeT;
252 
254  typedef std::map<DelayT,ValueT> MapT;
255 
256  //----------------------------------------------------------------------
257  // data
258 
263  MapT m_map;
264 
266  TimeT m_t;
267 
269  ValueT m_total;
270 
272  size_t m_n;
273 
274  //----------------------------------------------------------------------
275  // constructors etc.
276 
278  DDCTimeAvg(DelayT delay1=60, DelayT delay2=300, DelayT delay3=900, ValueT val=0)
279  : m_total(0), m_n(0)
280  {
281  SetDelays(delay1,delay2,delay3,val);
282  m_t.Set();
283  };
284 
286  DDCTimeAvg(const std::vector<DelayT>& delays, ValueT val=0)
287  : m_total(0), m_n(0)
288  {
289  SetDelays(delays,val);
290  m_t.Set();
291  };
292 
295  : m_map(X.m_map), m_t(X.m_t), m_total(X.m_total), m_n(X.m_n)
296  {};
297 
299  void reset(ValueT val=0);
300 
302  inline void clear()
303  {
304  m_map.clear();
305  reset();
306  };
307 
308  //----------------------------------------------------------------------
309  // manipulation
310 
312  void SetDelays(const std::vector<DelayT>& delays, ValueT val=0);
313 
315  void SetDelays(DelayT delay1=60, DelayT delay2=300, DelayT delay3=900, ValueT val=0);
316 
318  inline void SetDelays(const MapT& delayMap)
319  { m_map = delayMap; };
320 
324  MapT& Append(ValueT sample, const TimeT& when, size_t n=1);
325 
329  inline MapT& Append(ValueT sample=0)
330  { return Append(sample, TimeT::Now()); };
331 
333  inline void AppendTotal(ValueT sample)
334  { m_total += sample; };
335 
340  inline DDCTimeAvg& operator+=(const DDCTimeAvg& x)
341  { if (x.m_n > 0) Append(x.Total()); return *this;};
342 
344  inline void Scrub()
345  { Append(0,TimeT::Now(),0); };
346 
347  //----------------------------------------------------------------------
348  // access
349 
351  inline const MapT& Values() const
352  { return m_map; };
353 
355  inline const TimeT& Time() const
356  { return m_t; };
357 
359  inline ValueT Total() const
360  { return m_total; };
361 
363  inline ValueT Average() const
364  { return m_n==0 ? m_total : (m_total / (ValueT)m_n); };
365 
367  inline ValueT tAverage() const
368  { return m_map.begin()->second; };
369 
371  std::string toJsonArray() const;
372 
374  std::string toJsonMap() const;
375 };
376 
377 
378 
379 
380 //======================================================================
381 // Functions
382 
383 
384 #endif /* DDC_TIME_H */
385 
386 /*--- emacs style variables ---
387  * Local Variables:
388  * mode: C++
389  * c-file-style: "ellemtel"
390  * c-basic-offset: 4
391  * tab-width: 8
392  * indent-tabs-mode: nil
393  * End:
394  */
static DDCTimeHiRes Now()
Definition: ddcTime.h:159
size_t DelayT
Definition: ddcTime.h:245
DDCTimeHiRes TimeT
Definition: ddcTime.h:251
Definition: ddcTime.h:239
const TimeT & Time() const
Definition: ddcTime.h:355
ValueT Average() const
Definition: ddcTime.h:363
Definition: ddcTime.h:128
std::string TimeStr() const
Definition: ddcTime.h:114
time_t m_t
Definition: ddcTime.h:46
void Set(const struct timespec &ts_)
Definition: ddcTime.h:182
ValueT m_total
Definition: ddcTime.h:269
MapT m_map
Definition: ddcTime.h:263
void Set(const DDCTimeHiRes &t)
Definition: ddcTime.h:193
void Set(const time_t time_)
Definition: ddcTime.h:81
size_t m_n
Definition: ddcTime.h:272
struct timespec m_ts
Definition: ddcTime.h:133
std::string DateStr() const
Definition: ddcTime.h:106
virtual ~DDCTimeHiRes()
Definition: ddcTime.h:155
const size_t DDCTimeBufferSize
Definition: ddcTime.cpp:36
void AppendTotal(ValueT sample)
Definition: ddcTime.h:333
const size_t DDCDateBufferSize
Definition: ddcTime.cpp:33
DDCTimeHiRes(const DDCTimeHiRes &t)
Definition: ddcTime.h:147
DDCTimeAvg(DelayT delay1=60, DelayT delay2=300, DelayT delay3=900, ValueT val=0)
Definition: ddcTime.h:278
void Set(const DDCTimeUnix &t)
Definition: ddcTime.h:189
DDCTimeHiRes()
Definition: ddcTime.h:139
void Scrub()
Definition: ddcTime.h:344
void Set()
Definition: ddcTime.h:167
static DDCTimeUnix Now()
Definition: ddcTime.h:69
DDCTimeUnix(const time_t time_)
Definition: ddcTime.h:56
void Set(const DDCTimeUnix &t)
Definition: ddcTime.h:85
DDCTimeAvg & operator+=(const DDCTimeAvg &x)
Definition: ddcTime.h:340
DDCTimeUnix(const DDCTimeUnix &X)
Definition: ddcTime.h:60
ValueT Total() const
Definition: ddcTime.h:359
ValueT tAverage() const
Definition: ddcTime.h:367
DDCTimeHiRes(const DDCTimeUnix &t)
Definition: ddcTime.h:151
void Set(const time_t time_)
Definition: ddcTime.h:174
DDCTimeHiRes(const struct timespec &ts_)
Definition: ddcTime.h:143
void SetDelays(const MapT &delayMap)
Definition: ddcTime.h:318
DDCTimeAvg(const DDCTimeAvg &X)
Definition: ddcTime.h:294
double ValueT
Definition: ddcTime.h:248
const MapT & Values() const
Definition: ddcTime.h:351
virtual ~DDCTimeUnix()
Definition: ddcTime.h:65
void clear()
Definition: ddcTime.h:302
DDCTimeAvg(const std::vector< DelayT > &delays, ValueT val=0)
Definition: ddcTime.h:286
double Elapsed(const DDCTimeUnix &t0) const
Definition: ddcTime.cpp:74
void Set()
Definition: ddcTime.h:77
MapT & Append(ValueT sample=0)
Definition: ddcTime.h:329
Definition: ddcTime.h:41
std::map< DelayT, ValueT > MapT
Definition: ddcTime.h:254
time_t Time() const
Definition: ddcTime.h:92
TimeT m_t
Definition: ddcTime.h:266
virtual void DateTime(char *DateBuf, char *TimeBuf) const
Definition: ddcTime.cpp:62
DDCTimeUnix()
Definition: ddcTime.h:52