C++ main module for stochmagnet Package  1.0
CORE_Chrono.h
Go to the documentation of this file.
1 #ifndef CORE_Chrono_H
2 #define CORE_Chrono_H
3 
4 //inherits class header
5 #include "CORE_Object.h"
6 
7 //include the chrono header
8 #include <chrono>
9 //C time header
10 #include <ctime>
11 
18 class CORE_Chrono : public CORE_Object {
19  // ATTRIBUTES
20 
21  public:
22 
23 
24 private:
25  // ASSOCIATION
26  //timer
27  std::chrono::high_resolution_clock mTimer;
28  //current time
29  std::chrono::time_point<std::chrono::high_resolution_clock> mStart;
30 
31  tUInt mYear;//in [1900, ...[
32  tCInt mMonth;//in [0,11]
33  tUCInt mDay;//day in [1,31]
34  tUCInt mWeekDay;//day in [0,6]
35  tUCInt mHour;//in [0,23]
36  tUCInt mMinute;//in [0,59]
37  tUCInt mSecond;//in [0,59]
38 
39  // METHODS
40 
41 
42 public:
43  // CONSTRUCTORS
47  auto now=std::chrono::system_clock::now();
48  time_t tnow = std::chrono::system_clock::to_time_t(now);
49 
50  tm* ptm = localtime(&tnow);
51  mYear= ptm->tm_year + 1900;
52  mMonth = ptm->tm_mon ;
53  mDay= ptm->tm_mday;
54  mHour = ptm->tm_hour;
55  mMinute = ptm->tm_min;
56  mSecond= ptm->tm_sec;
57  mWeekDay=ptm->tm_wday;
58 
59 
60  }
61 
62 
63 
64 
65  // DESTRUCTORS
69  virtual ~CORE_Chrono(void) {
70  }
71 
72 
73 public:
74 
75 
76 private:
77 
78 
79 public:
80 
88  virtual tMemSize getMemorySize() const override{
89  return sizeof(*this)+getContentsMemorySize();
90  }
91 
92 public:
95  inline void start() {
96  mStart=mTimer.now();
97  }
100  inline std::chrono::duration<tReal> stop() {
101  auto end=mTimer.now();
102  std::chrono::duration<tReal> duration= end-mStart;
103  mStart=end;
104  return duration;
105  }
106 
107  /* \brief get the clock time
108  * @return the time
109  */
110  inline static tULLInt GetClockTime() {
111  return std::clock();
112  }
113  /* \brief get the clock time duration
114  * @param[in] start_time : start time in clock
115  * @return the duration in milliseconds
116  */
117  inline static tULLInt GetClockDuration(const tULLInt& startTime) {
118  return (1000*(std::clock()-startTime))/CLOCKS_PER_SEC;
119  }
123  static inline tULLInt ConvertDuration(std::chrono::duration<tReal>&& duration) {
124  return std::chrono::duration_cast< std::chrono::milliseconds >( duration ).count();
125  }
129  static inline tULLInt ConvertDuration(const std::chrono::duration<tReal>& duration) {
130  return std::chrono::duration_cast< std::chrono::milliseconds >( duration ).count();
131  }
132 
147  static tReal ConvertDuration(std::chrono::duration<tReal>&& duration,
148  tUInt& d,
149  tUInt& h,
150  tUInt& m,
151  tUInt& s,
152  tUInt& ms,
153  tUInt& mus,
154  tUInt& ns) {
155 
156  tReal ret=duration.count();
157 
158  auto hours = std::chrono::duration_cast< std::chrono::hours >( duration );
159  h=hours.count();
160  d=h/24;
161  h=h%24;
162  duration -= hours;
163 
164  auto minutes = std::chrono::duration_cast< std::chrono::minutes >( duration );
165  m=minutes.count();
166  duration -= minutes;
167 
168  auto seconds = std::chrono::duration_cast< std::chrono::seconds >( duration );
169  s=seconds.count();
170  duration -= seconds;
171 
172  auto milliseconds = std::chrono::duration_cast< std::chrono::milliseconds >( duration );
173  ms=milliseconds.count();
174  duration -= milliseconds;
175 
176  auto microseconds = std::chrono::duration_cast< std::chrono::microseconds >( duration );
177  mus=microseconds.count();
178  duration -= microseconds;
179 
180  auto nanoseconds = std::chrono::duration_cast< std::chrono::nanoseconds >( duration );
181  ns=nanoseconds.count();
182 
183  return ret;
184  }
185 
200  static tReal ConvertDuration(std::chrono::duration<tReal>& duration,
201  tUInt& d,
202  tUInt& h,
203  tUInt& m,
204  tUInt& s,
205  tUInt& ms,
206  tUInt& mus,
207  tUInt& ns) {
208 
209 
210 
211  tReal ret=duration.count();
212 
213  auto hours = std::chrono::duration_cast< std::chrono::hours >( duration );
214  h=hours.count();
215  d=h/24;
216  h=h%24;
217  duration -= hours;
218 
219  auto minutes = std::chrono::duration_cast< std::chrono::minutes >( duration );
220  m=minutes.count();
221  duration -= minutes;
222 
223  auto seconds = std::chrono::duration_cast< std::chrono::seconds >( duration );
224  s=seconds.count();
225  duration -= seconds;
226 
227  auto milliseconds = std::chrono::duration_cast< std::chrono::milliseconds >( duration );
228  ms=milliseconds.count();
229  duration -= milliseconds;
230 
231  auto microseconds = std::chrono::duration_cast< std::chrono::microseconds >( duration );
232  mus=microseconds.count();
233  duration -= microseconds;
234 
235  auto nanoseconds = std::chrono::duration_cast< std::chrono::nanoseconds >( duration );
236  ns=nanoseconds.count();
237 
238  return ret;
239  }
243  inline tString getDate() {
244  std::stringstream cstr;
245 
246  cstr<<mYear<<"/";
247  if (mMonth<9) cstr<<"0";
248  cstr<<((int)mMonth+1)<<"-";
249  if (mDay<10) cstr<<"0";
250  cstr<<((int)mDay);
251  cstr<<"/";
252  if (mHour<10) cstr<<"0";
253  cstr<<((int)mHour)<<":";
254  if (mMinute<10) cstr<<"0";
255  cstr<<((int)mMinute)<<":";
256  if (mSecond<10) cstr<<"0";
257  cstr<<((int)mSecond);
258  return cstr.str();
259  }
260 
261  inline static tString GetDate() {
262  CORE_Chrono time;
263  return time.getDate();
264  }
265 
266 private:
267 
268 
269 };
270 
271 #endif
this class describes the chono class by default write on standart output
Definition: CORE_Chrono.h:18
std::chrono::high_resolution_clock mTimer
Definition: CORE_Chrono.h:27
std::chrono::duration< tReal > stop()
stop the chrono and return the duration time in seconds as a real
Definition: CORE_Chrono.h:100
CORE_Chrono()
build a CORE_Chrono
Definition: CORE_Chrono.h:46
static tReal ConvertDuration(std::chrono::duration< tReal > &duration, tUInt &d, tUInt &h, tUInt &m, tUInt &s, tUInt &ms, tUInt &mus, tUInt &ns)
convert the duration as days,hours,minutes,second,milliseconds,microseconds
Definition: CORE_Chrono.h:200
tUCInt mHour
Definition: CORE_Chrono.h:35
std::chrono::time_point< std::chrono::high_resolution_clock > mStart
Definition: CORE_Chrono.h:29
tCInt mMonth
Definition: CORE_Chrono.h:32
tUInt mYear
Definition: CORE_Chrono.h:31
static tULLInt ConvertDuration(std::chrono::duration< tReal > &&duration)
convert duration in ms @retunr the duration in milli seconds
Definition: CORE_Chrono.h:123
static tString GetDate()
Definition: CORE_Chrono.h:261
tUCInt mDay
Definition: CORE_Chrono.h:33
tUCInt mWeekDay
Definition: CORE_Chrono.h:34
tUCInt mSecond
Definition: CORE_Chrono.h:37
static tULLInt GetClockTime()
Definition: CORE_Chrono.h:110
static tULLInt ConvertDuration(const std::chrono::duration< tReal > &duration)
convert duration in ms @retunr the duration in milli seconds
Definition: CORE_Chrono.h:129
tString getDate()
get the date in string
Definition: CORE_Chrono.h:243
void start()
start the chrono
Definition: CORE_Chrono.h:95
static tULLInt GetClockDuration(const tULLInt &startTime)
Definition: CORE_Chrono.h:117
tUCInt mMinute
Definition: CORE_Chrono.h:36
static tReal ConvertDuration(std::chrono::duration< tReal > &&duration, tUInt &d, tUInt &h, tUInt &m, tUInt &s, tUInt &ms, tUInt &mus, tUInt &ns)
convert the duration as days,hours,minutes,second,milliseconds,microseconds
Definition: CORE_Chrono.h:147
virtual tMemSize getMemorySize() const override
return the memory size of the class and the memory size of all its attributes/associations
Definition: CORE_Chrono.h:88
virtual ~CORE_Chrono(void)
destroy a CORE_Chrono
Definition: CORE_Chrono.h:69
abstract base class for most classes.
Definition: CORE_Object.h:48
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:259
#define tUInt
Definition: types.h:44
#define tString
Definition: types.h:147
#define tULLInt
Definition: types.h:58
#define tMemSize
Definition: types.h:166
#define tUCInt
Definition: types.h:26
#define tCInt
Definition: types.h:34
#define tReal
Definition: types.h:137