GestureRecognitionToolkit  Version: 1.0 Revision: 04-03-15
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 = Util::stringToInt( s[0] );
463  month = Util::stringToInt( s[1] );
464  day = Util::stringToInt( s[2] );
465  hour = Util::stringToInt( s[3] );
466  minute = Util::stringToInt( s[4] );
467  second = Util::stringToInt( s[5] );
468  millisecond = Util::stringToInt( s[6] );
469 
470  return true;
471  }
472 
473  std::string getTimeStampAsString() const{
474  std::string timeString = "";
475  timeString += Util::toString(year) + "_" + Util::toString(month) + "_" + Util::toString(day);
476  timeString += "_" + Util::toString(hour) + "_" + Util::toString(minute) + "_" + Util::toString(second) + "_" + Util::toString(millisecond);
477  return timeString;
478  }
479 
480  std::string getTimeStampAsJSONString() const{
481  std::string timeString = "{";
482  timeString += "\"year\":" + Util::toString(year) + ",";
483  timeString += "\"month\":" + Util::toString(month) + ",";
484  timeString += "\"day\":" + Util::toString(day) + ",";
485  timeString += "\"hour\":" + Util::toString(hour) + ",";
486  timeString += "\"minute\":" + Util::toString(minute) + ",";
487  timeString += "\"second\":" + Util::toString(second) + ",";
488  timeString += "\"millisecond\":" + Util::toString(millisecond) + ",";
489  timeString += "\"timeInMS\":" + Util::toString(getTimeInMilliseconds());
490  timeString += "}";
491  return timeString;
492  }
493 
494  std::string getTimeAsISOString() const{
495  std::string s = "";
496  s += Util::toString(year) + "-";
497  s += pad( Util::toString(month) ) + "-";
498  s += pad( Util::toString(day) ) + "T";
499  s += pad( Util::toString( hour ) ) + ":";
500  s += pad( Util::toString( minute ) ) + ":";
501  s += pad( Util::toString( second ) ) + ".";
502  s += Util::toString( millisecond ) + "Z";
503  return s;
504  }
505 
506  unsigned int getNumDaysInMonth( const unsigned int month ){
507  switch( month ){
508  case 1://Jan
509  return 31;
510  break;
511  case 2: //Feb
512  return 29; //Leap Year?????
513  break;
514  case 3: //March
515  return 31;
516  break;
517  case 4: //April
518  return 30;
519  break;
520  case 5: //May
521  return 31;
522  break;
523  case 6: //June
524  return 30;
525  break;
526  case 7: //July
527  return 31;
528  break;
529  case 8: //August
530  return 31;
531  break;
532  case 9: //September
533  return 31;
534  break;
535  case 10: //October
536  return 31;
537  break;
538  case 11: //November
539  return 30;
540  break;
541  case 12: //December
542  return 31;
543  break;
544  }
545  warningLog << "getNumDaysInMonth(const unsigned int month) - Bad month parameter: " << month << std::endl;
546  return 0;
547  }
548 
549  std::string pad(const std::string s) const {
550 
551  if( s.length() != 1 ) return s;
552  return ( "0" + s );
553  }
554 
555  unsigned int year;
556  unsigned int month;
557  unsigned int day;
558  unsigned int hour;
559  unsigned int minute;
560  unsigned int second;
561  unsigned int millisecond;
562  ErrorLog errorLog;
563  WarningLog warningLog;
564 
565 };
566 
567 } //End of namespace GRT
568 #endif //GRT_TIMESTAMP_HEADER
static std::string toString(const int &i)
Definition: Util.cpp:65
Definition: AdaBoost.cpp:25
static int stringToInt(const std::string &s)
Definition: Util.cpp:117