GestureRecognitionToolkit  Version: 0.2.5
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
TimeStamp.h
1 /*
2  GRT MIT License
3  Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5  Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6  and associated documentation files (the "Software"), to deal in the Software without restriction,
7  including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9  subject to the following conditions:
10 
11  The above copyright notice and this permission notice shall be included in all copies or substantial
12  portions of the Software.
13 
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15  LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #ifndef GRT_TIMESTAMP_HEADER
22 #define GRT_TIMESTAMP_HEADER
23 
24 #include "Timer.h"
25 #include "ErrorLog.h"
26 #include "WarningLog.h"
27 
28 GRT_BEGIN_NAMESPACE
29 
30 class TimeStamp{
31 public:
32  TimeStamp(unsigned int year=0,unsigned int month=0,unsigned int day=0,unsigned int hour=0,unsigned int minute=0,unsigned int second=0,unsigned int millisecond=0){
33  this->year = year;
34  this->month = month;
35  this->day = day;
36  this->hour = hour;
37  this->minute = minute;
38  this->second = second;
39  this->millisecond = millisecond;
40 
41  errorLog.setKey("[ERROR TimeStamp]");
42  warningLog.setKey("[WARNING TimeStamp]");
43  }
44 
45  TimeStamp(const TimeStamp &rhs){
46  *this = rhs;
47  }
48 
49  TimeStamp(const std::string &timeStampAsString){
50  this->year = 0;
51  this->month = 0;
52  this->day = 0;
53  this->hour = 0;
54  this->minute = 0;
55  this->second = 0;
56  this->millisecond = 0;
57  setTimeStampFromString( timeStampAsString );
58 
59  errorLog.setKey("[ERROR TimeStamp]");
60  warningLog.setKey("[WARNING TimeStamp]");
61  }
62 
63  ~TimeStamp(){
64 
65  }
66 
67  TimeStamp& operator=(const TimeStamp &rhs){
68  if( this != &rhs ){
69  this->year = rhs.year;
70  this->month = rhs.month;
71  this->day = rhs.day;
72  this->hour = rhs.hour;
73  this->minute = rhs.minute;
74  this->second = rhs.second;
75  this->millisecond = rhs.millisecond;
76  this->errorLog = rhs.errorLog;
77  this->warningLog = rhs.warningLog;
78  }
79  return *this;
80  }
81 
82  bool operator>(const TimeStamp &rhs) const{
83  if( this->getTimeInMilliseconds() > rhs.getTimeInMilliseconds() ){
84  return true;
85  }
86  return false;
87  }
88 
89  bool operator>=(const TimeStamp &rhs) const{
90  if( this->getTimeInMilliseconds() >= rhs.getTimeInMilliseconds() ){
91  return true;
92  }
93  return false;
94  }
95 
96  bool operator<(const TimeStamp &rhs) const{
97  if( this->getTimeInMilliseconds() < rhs.getTimeInMilliseconds() ){
98  return true;
99  }
100  return false;
101  }
102 
103  bool operator<=(const TimeStamp &rhs) const{
104  if( this->getTimeInMilliseconds() <= rhs.getTimeInMilliseconds() ){
105  return true;
106  }
107  return false;
108  }
109 
110  TimeStamp operator+(const TimeStamp &rhs){
111  TimeStamp ts;
112  ts.year = this->year + rhs.year;
113  ts.month = this->month + rhs.month;
114  ts.day = this->day + rhs.day;
115  ts.hour = this->hour + rhs.hour;
116  ts.minute = this->minute + rhs.minute;
117  ts.second = this->second + rhs.second;
118  ts.millisecond = this->millisecond + rhs.millisecond;
119 
120  if( ts.millisecond >= 1000 ){
121  ts.millisecond = ts.millisecond % 1000;
122  ts.second++;
123  }
124 
125  if( ts.second >= 60 ){
126  ts.second = ts.second % 60;
127  ts.minute++;
128  }
129 
130  if( ts.minute >= 60 ){
131  ts.minute = ts.minute % 60;
132  ts.hour++;
133  }
134 
135  if( ts.hour >= 24 ){
136  ts.hour = ts.hour % 24;
137  ts.day++;
138  }
139 
140  unsigned int numDaysInMonth = 0;
141  if( ts.month > 12 ){
142  numDaysInMonth = getNumDaysInMonth( ts.month % 12 );
143  }else numDaysInMonth = getNumDaysInMonth( ts.month );
144 
145  if( ts.day >= numDaysInMonth ){
146  ts.day = ts.day - numDaysInMonth;
147  ts.month++;
148  }
149 
150  if( ts.month > 12 ){
151  ts.month = ts.month % 12;
152  ts.year++;
153  }
154 
155  return ts;
156  }
157 
158  TimeStamp& operator+=(const TimeStamp &rhs){
159  if( this != &rhs ){
160  this->year += rhs.year;
161  this->month += rhs.month;
162  this->day += rhs.day;
163  this->hour += rhs.hour;
164  this->minute += rhs.minute;
165  this->second += rhs.second;
166  this->millisecond += rhs.millisecond;
167 
168  if( this->millisecond >= 1000 ){
169  this->millisecond = this->millisecond % 1000;
170  this->second++;
171  }
172 
173  if( this->second >= 60 ){
174  this->second = this->second % 60;
175  this->minute++;
176  }
177 
178  if( this->minute >= 60 ){
179  this->minute = this->minute % 60;
180  this->hour++;
181  }
182 
183  if( this->hour >= 24 ){
184  this->hour = this->hour % 24;
185  this->day++;
186  }
187 
188  unsigned int numDaysInMonth = 0;
189  if( this->month > 12 ){
190  numDaysInMonth = getNumDaysInMonth( this->month % 12 );
191  }else numDaysInMonth = getNumDaysInMonth( this->month );
192 
193  if( this->day >= numDaysInMonth ){
194  this->day = this->day - numDaysInMonth;
195  this->month++;
196  }
197 
198  if( this->month > 12 ){
199  this->month = this->month % 12;
200  this->year++;
201  }
202  }
203  return *this;
204  }
205 
206  TimeStamp operator-(const TimeStamp &rhs){
207 
208  int year = (int)this->year - rhs.year;
209  int month = (int)this->month - rhs.month;
210  int day = (int)this->day - rhs.day;
211  int hour = (int)this->hour - rhs.hour;
212  int minute = (int)this->minute - rhs.minute;
213  int second = (int)this->second - rhs.second;
214  int millisecond = (int)this->millisecond - rhs.millisecond;
215 
216  if( millisecond < 0 ){
217  millisecond = this->millisecond + 1000 - rhs.millisecond;
218  second--;
219  }
220 
221  if( second < 0 ){
222  second = this->second + 60 - rhs.second;
223  minute--;
224  }
225 
226  if( minute < 0 ){
227  minute = this->minute + 60 - rhs.minute;
228  hour--;
229  }
230 
231  if( hour < 0 ){
232  hour = this->hour + 24 - rhs.hour;
233  day--;
234  }
235 
236  if( day <= 0 ){
237  int numDaysInMonth = 0;
238  if( month > 1 ){
239  numDaysInMonth = getNumDaysInMonth( month - 1 );
240  }else numDaysInMonth = getNumDaysInMonth( 12 - month );
241 
242  day = numDaysInMonth - day;
243  month--;
244  }
245 
246  if( month <= 0 ){
247  month = 12 - month;
248  year--;
249  }
250 
251  TimeStamp ts;
252  ts.year = year;
253  ts.month = month;
254  ts.day = day;
255  ts.hour = hour;
256  ts.minute = minute;
257  ts.second = second;
258  ts.millisecond = millisecond;
259 
260  return ts;
261  }
262 
263  TimeStamp& operator-=(const TimeStamp &rhs){
264  if( this != &rhs ){
265 
266  int year = (int)this->year - rhs.year;
267  int month = (int)this->month - rhs.month;
268  int day = (int)this->day - rhs.day;
269  int hour = (int)this->hour - rhs.hour;
270  int minute = (int)this->minute - rhs.minute;
271  int second = (int)this->second - rhs.second;
272  int millisecond = (int)this->millisecond - rhs.millisecond;
273 
274  if( millisecond < 0 ){
275  millisecond = this->millisecond + 1000 - rhs.millisecond;
276  second--;
277  }
278 
279  if( second < 0 ){
280  second = this->second + 60 - rhs.second;
281  minute--;
282  }
283 
284  if( minute < 0 ){
285  minute = this->minute + 60 - rhs.minute;
286  hour--;
287  }
288 
289  if( hour < 0 ){
290  hour = this->hour + 24 - rhs.hour;
291  day--;
292  }
293 
294  if( day <= 0 ){
295  int numDaysInMonth = 0;
296  if( month > 1 ){
297  numDaysInMonth = getNumDaysInMonth( month - 1 );
298  }else numDaysInMonth = getNumDaysInMonth( 12 - month );
299 
300  day = numDaysInMonth - day;
301  month--;
302  }
303 
304  if( month <= 0 ){
305  month = 12 - month;
306  year--;
307  }
308 
309  this->year = year;
310  this->month = month;
311  this->day = day;
312  this->hour = hour;
313  this->minute = minute;
314  this->second = second;
315  this->millisecond = millisecond;
316  }
317  return *this;
318  }
319 
320  unsigned long long getTimeInMilliseconds() const{
321 
322  unsigned long long secondInMs = 1000;
323  unsigned long long minuteInMs = 60*secondInMs;
324  unsigned long long hourInMs = 60*minuteInMs;
325  unsigned long long dayInMs = 24*hourInMs;
326 
327  unsigned long long firstJan2014InMS = 1388534400000; //Number of milliseconds since Jan 01 1970 on 01/01/2014 at 00:00:00
328  unsigned long long yearTime = year == 2014 ? firstJan2014InMS : 0;
329  unsigned long long monthTime = 0;
330  unsigned long long dayTime = day == 1 ? 0 : (day-1) * dayInMs;
331  unsigned long long hourTime = hour == 0 ? 0 : (hour-1) * hourInMs;
332  unsigned long long minuteTime = minute == 0 ? 0 : (minute-1) * minuteInMs;
333  unsigned long long secondTime = second == 0 ? 0 : (second-1) * secondInMs;
334 
335  unsigned long long janInMs = 31*dayInMs;
336  unsigned long long febInMs = 29*dayInMs + janInMs;
337  unsigned long long marchInMs = 31*dayInMs + febInMs;
338  unsigned long long aprilInMs = 30*dayInMs + marchInMs;
339  unsigned long long mayInMs = 31*dayInMs + aprilInMs;
340  unsigned long long juneInMs = 30*dayInMs + mayInMs;
341  unsigned long long julyInMs = 31*dayInMs + juneInMs;
342  unsigned long long augInMs = 31*dayInMs + julyInMs;
343  unsigned long long sepInMs = 31*dayInMs + augInMs;
344  unsigned long long octInMs = 31*dayInMs + sepInMs;
345  unsigned long long novInMs = 30*dayInMs + octInMs;
346 
347  switch( month ){
348  case 1:
349  monthTime = 0;
350  break;
351  case 2:
352  monthTime = janInMs;
353  break;
354  case 3:
355  monthTime = febInMs;
356  break;
357  case 4:
358  monthTime = marchInMs;
359  break;
360  case 5:
361  monthTime = aprilInMs;
362  break;
363  case 6:
364  monthTime = mayInMs;
365  break;
366  case 7:
367  monthTime = juneInMs;
368  break;
369  case 8:
370  monthTime = julyInMs;
371  break;
372  case 9:
373  monthTime = augInMs;
374  break;
375  case 10:
376  monthTime = sepInMs;
377  break;
378  case 11:
379  monthTime = octInMs;
380  break;
381  case 12:
382  monthTime = novInMs;
383  break;
384  }
385 
386  return (yearTime + monthTime + dayTime + hourTime + minuteTime + secondTime + millisecond);
387  }
388 
389  unsigned int getDayTimeInMilliseconds() const{
390 
391  unsigned int secondInMs = 1000;
392  unsigned int minuteInMs = 60*secondInMs;
393  unsigned int hourInMs = 60*minuteInMs;
394  unsigned int hourTime = hour * hourInMs;
395  unsigned int minuteTime = minute * minuteInMs;
396  unsigned int secondTime = second * secondInMs;
397  return (hourTime + minuteTime + secondTime + millisecond);
398  }
399 
400  bool setTimeStampAsNow(){
401 #if defined(__GRT_OSX_BUILD__) || defined(__GRT_LINUX_BUILD__)
402 
403  //Get the date and time
404  time_t tim = time(NULL);
405  tm *now = localtime( &tim );
406 
407  if( now == NULL ) return false;
408 
409  //Get the millisecon time
410  struct timeval nowTimeval;
411  gettimeofday( &nowTimeval, NULL );
412 
413  year = (unsigned int)now->tm_year + 1900;
414  month = (unsigned int)now->tm_mon + 1;
415  day = (unsigned int)now->tm_mday;
416  hour = (unsigned int)now->tm_hour;
417  minute = (unsigned int)now->tm_min;
418  second = (unsigned int)now->tm_sec;
419  millisecond = (unsigned int)nowTimeval.tv_usec/1000;
420 
421  return true;
422 #endif
423 #ifdef __GRT_WINDOWS_BUILD__
424  SYSTEMTIME systemTime;
425  GetSystemTime(&systemTime);
426  year = (unsigned int)systemTime.wYear;
427  month = (unsigned int)systemTime.wMonth;
428  day = (unsigned int)systemTime.wDay;
429  hour = (unsigned int)systemTime.wHour;
430  minute = (unsigned int)systemTime.wMinute;
431  second = (unsigned int)systemTime.wSecond;
432  millisecond = (unsigned int)systemTime.wMilliseconds;
433  return true;
434 #endif
435  warningLog << "setTimeStampAsNow() - Failed to get time stamp value! Unknown OS!" << std::endl;
436  return false;
437  }
438 
439  bool setTimeStampFromString(const std::string &timeString){
440 
441  if( timeString == "NOW" || timeString == "now" ){
442  return setTimeStampAsNow();
443  }
444 
445  //Find all the _
446  std::vector< std::string > s;
447  std::string tempString;
448  for(unsigned int i=0; i<timeString.length(); i++ ){
449  if( timeString[i] == '_' || timeString[i] == '\n' || timeString[i] == '\r' ){
450  s.push_back( tempString );
451  tempString = "";
452  }else tempString += timeString[i];
453  }
454 
455  if( tempString.size() > 0 ) s.push_back( tempString );
456 
457  if( s.size() != 7 ){
458  warningLog << "WARNING: Failed to set timestamp from string. Incorrect size! Size: " << s.size() << std::endl;
459  return false;
460  }
461 
462  year = grt_from_str< unsigned int >( s[0] );
463  month = grt_from_str <unsigned int >( s[1] );
464  day = grt_from_str< unsigned int >( s[2] );
465  hour = grt_from_str< unsigned int >( s[3] );
466  minute = grt_from_str< unsigned int >( s[4] );
467  second = grt_from_str< unsigned int >( s[5] );
468  millisecond = grt_from_str< unsigned int >( s[6] );
469 
470  return true;
471  }
472 
473  std::string getTimeStampAsString( const bool includeDate = true ) const {
474  std::string timeString = "";
475  if( includeDate ){
476  timeString = grt_to_str< unsigned int >(year);
477  timeString += grt_to_str("_");
478  timeString += grt_to_str< unsigned int >(month);
479  timeString += grt_to_str("_");
480  timeString += grt_to_str< unsigned int >(day);
481  timeString += grt_to_str("_");
482  }
483  timeString += grt_to_str< unsigned int >(hour);
484  timeString += grt_to_str("_");
485  timeString += grt_to_str< unsigned int >(minute);
486  timeString += grt_to_str("_");
487  timeString += grt_to_str< unsigned int >(second);
488  timeString += grt_to_str("_");
489  timeString += grt_to_str< unsigned int >(millisecond);
490  return timeString;
491  }
492 
493  std::string getTimeStampAsJSONString() const {
494  std::string timeString = "{";
495  timeString += grt_to_str("\"year\":");
496  timeString += grt_to_str< unsigned int >(year);
497  timeString += grt_to_str(",");
498  timeString += grt_to_str("\"month\":");
499  timeString += grt_to_str< unsigned int >(month);
500  timeString += grt_to_str(",");
501  timeString += grt_to_str("\"day\":");
502  timeString += grt_to_str< unsigned int >(day);
503  timeString += grt_to_str(",");
504  timeString += grt_to_str("\"hour\":");
505  timeString += grt_to_str< unsigned int >(hour);
506  timeString += grt_to_str(",");
507  timeString += grt_to_str("\"minute\":");
508  timeString += grt_to_str< unsigned int >(minute);
509  timeString += grt_to_str(",");
510  timeString += grt_to_str("\"second\":");
511  timeString += grt_to_str< unsigned int >(second);
512  timeString += grt_to_str(",");
513  timeString += grt_to_str("\"millisecond\":");
514  timeString += grt_to_str< unsigned int >(millisecond);
515  timeString += grt_to_str(",");
516  timeString += grt_to_str("\"timeInMS\":");
517  timeString += grt_to_str< unsigned int >(
518  static_cast<unsigned int>(
519  getTimeInMilliseconds()
520  )
521  );
522  timeString += grt_to_str("}");
523  return timeString;
524  }
525 
526  std::string getTimeAsISOString() const {
527  std::string s = "";
528  s += grt_to_str< unsigned int >(year);
529  s += grt_to_str("-");
530  s += pad( grt_to_str< unsigned int >(month) );
531  s += grt_to_str("-");
532  s += pad( grt_to_str< unsigned int >(day) );
533  s += grt_to_str("T");
534  s += pad( grt_to_str< unsigned int >( hour ) );
535  s += grt_to_str(":");
536  s += pad( grt_to_str< unsigned int >( minute ) );
537  s += grt_to_str(":");
538  s += pad( grt_to_str< unsigned int >( second ) );
539  s += grt_to_str(".");
540  s += grt_to_str< unsigned int >( millisecond );
541  s += grt_to_str("Z");
542  return s;
543  }
544 
545  unsigned int getNumDaysInMonth( const unsigned int month ){
546  switch( month ){
547  case 1://Jan
548  return 31;
549  break;
550  case 2: //Feb
551  return 29; //Leap Year?????
552  break;
553  case 3: //March
554  return 31;
555  break;
556  case 4: //April
557  return 30;
558  break;
559  case 5: //May
560  return 31;
561  break;
562  case 6: //June
563  return 30;
564  break;
565  case 7: //July
566  return 31;
567  break;
568  case 8: //August
569  return 31;
570  break;
571  case 9: //September
572  return 31;
573  break;
574  case 10: //October
575  return 31;
576  break;
577  case 11: //November
578  return 30;
579  break;
580  case 12: //December
581  return 31;
582  break;
583  }
584  warningLog << "getNumDaysInMonth(const unsigned int month) - Bad month parameter: " << month << std::endl;
585  return 0;
586  }
587 
588  std::string pad(const std::string &s) const {
589  if( s.length() != 1 ) return s;
590  return ( "0" + s );
591  }
592 
593  unsigned int year;
594  unsigned int month;
595  unsigned int day;
596  unsigned int hour;
597  unsigned int minute;
598  unsigned int second;
599  unsigned int millisecond;
600  ErrorLog errorLog;
601  WarningLog warningLog;
602 
603 };
604 
605 GRT_END_NAMESPACE
606 
607 #endif //GRT_TIMESTAMP_HEADER
virtual bool setKey(const std::string &key)
sets the key that gets written at the start of each message, this will be written in the format &#39;key ...
Definition: Log.h:166