GestureRecognitionToolkit  Version: 0.1.0
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 namespace GRT{
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.setProceedingText("[ERROR TimeStamp]");
42  warningLog.setProceedingText("[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.setProceedingText("[ERROR TimeStamp]");
60  warningLog.setProceedingText("[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 {
474  std::string timeString = grt_to_str(year);
475  timeString += grt_to_str("_");
476  timeString += grt_to_str(month);
477  timeString += grt_to_str("_");
478  timeString += grt_to_str(day);
479  timeString += grt_to_str("_");
480  timeString += grt_to_str(hour);
481  timeString += grt_to_str("_");
482  timeString += grt_to_str(minute);
483  timeString += grt_to_str("_");
484  timeString += grt_to_str(second);
485  timeString += grt_to_str("_");
486  timeString += grt_to_str(millisecond);
487  return timeString;
488  }
489 
490  std::string getTimeStampAsJSONString() const {
491  std::string timeString = "{";
492  timeString += grt_to_str("\"year\":");
493  timeString += grt_to_str(year);
494  timeString += grt_to_str(",");
495  timeString += grt_to_str("\"month\":");
496  timeString += grt_to_str(month);
497  timeString += grt_to_str(",");
498  timeString += grt_to_str("\"day\":");
499  timeString += grt_to_str(day);
500  timeString += grt_to_str(",");
501  timeString += grt_to_str("\"hour\":");
502  timeString += grt_to_str(hour);
503  timeString += grt_to_str(",");
504  timeString += grt_to_str("\"minute\":");
505  timeString += grt_to_str(minute);
506  timeString += grt_to_str(",");
507  timeString += grt_to_str("\"second\":");
508  timeString += grt_to_str(second);
509  timeString += grt_to_str(",");
510  timeString += grt_to_str("\"millisecond\":");
511  timeString += grt_to_str(millisecond);
512  timeString += grt_to_str(",");
513  timeString += grt_to_str("\"timeInMS\":");
514  timeString += grt_to_str(getTimeInMilliseconds());
515  timeString += grt_to_str("}");
516  return timeString;
517  }
518 
519  std::string getTimeAsISOString() const {
520  std::string s = "";
521  s += grt_to_str(year);
522  s += grt_to_str("-");
523  s += pad( grt_to_str(month) );
524  s += grt_to_str("-");
525  s += pad( grt_to_str(day) );
526  s += grt_to_str("T");
527  s += pad( grt_to_str( hour ) );
528  s += grt_to_str(":");
529  s += pad( grt_to_str( minute ) );
530  s += grt_to_str(":");
531  s += pad( grt_to_str( second ) );
532  s += grt_to_str(".");
533  s += grt_to_str( millisecond );
534  s += grt_to_str("Z");
535  return s;
536  }
537 
538  unsigned int getNumDaysInMonth( const unsigned int month ){
539  switch( month ){
540  case 1://Jan
541  return 31;
542  break;
543  case 2: //Feb
544  return 29; //Leap Year?????
545  break;
546  case 3: //March
547  return 31;
548  break;
549  case 4: //April
550  return 30;
551  break;
552  case 5: //May
553  return 31;
554  break;
555  case 6: //June
556  return 30;
557  break;
558  case 7: //July
559  return 31;
560  break;
561  case 8: //August
562  return 31;
563  break;
564  case 9: //September
565  return 31;
566  break;
567  case 10: //October
568  return 31;
569  break;
570  case 11: //November
571  return 30;
572  break;
573  case 12: //December
574  return 31;
575  break;
576  }
577  warningLog << "getNumDaysInMonth(const unsigned int month) - Bad month parameter: " << month << std::endl;
578  return 0;
579  }
580 
581  std::string pad(const std::string &s) const {
582  if( s.length() != 1 ) return s;
583  return ( "0" + s );
584  }
585 
586  unsigned int year;
587  unsigned int month;
588  unsigned int day;
589  unsigned int hour;
590  unsigned int minute;
591  unsigned int second;
592  unsigned int millisecond;
593  ErrorLog errorLog;
594  WarningLog warningLog;
595 
596 };
597 
598 } //End of namespace GRT
599 #endif //GRT_TIMESTAMP_HEADER
Definition: DebugLog.cpp:23