21 #ifndef GRT_TIMESTAMP_HEADER 22 #define GRT_TIMESTAMP_HEADER 26 #include "WarningLog.h" 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){
37 this->minute = minute;
38 this->second = second;
39 this->millisecond = millisecond;
41 errorLog.
setKey(
"[ERROR TimeStamp]");
42 warningLog.setKey(
"[WARNING TimeStamp]");
49 TimeStamp(
const std::string &timeStampAsString){
56 this->millisecond = 0;
57 setTimeStampFromString( timeStampAsString );
59 errorLog.
setKey(
"[ERROR TimeStamp]");
60 warningLog.setKey(
"[WARNING TimeStamp]");
69 this->year = rhs.year;
70 this->month = rhs.month;
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;
82 bool operator>(
const TimeStamp &rhs)
const{
83 if( this->getTimeInMilliseconds() > rhs.getTimeInMilliseconds() ){
89 bool operator>=(
const TimeStamp &rhs)
const{
90 if( this->getTimeInMilliseconds() >= rhs.getTimeInMilliseconds() ){
96 bool operator<(
const TimeStamp &rhs)
const{
97 if( this->getTimeInMilliseconds() < rhs.getTimeInMilliseconds() ){
103 bool operator<=(
const TimeStamp &rhs)
const{
104 if( this->getTimeInMilliseconds() <= rhs.getTimeInMilliseconds() ){
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;
120 if( ts.millisecond >= 1000 ){
121 ts.millisecond = ts.millisecond % 1000;
125 if( ts.second >= 60 ){
126 ts.second = ts.second % 60;
130 if( ts.minute >= 60 ){
131 ts.minute = ts.minute % 60;
136 ts.hour = ts.hour % 24;
140 unsigned int numDaysInMonth = 0;
142 numDaysInMonth = getNumDaysInMonth( ts.month % 12 );
143 }
else numDaysInMonth = getNumDaysInMonth( ts.month );
145 if( ts.day >= numDaysInMonth ){
146 ts.day = ts.day - numDaysInMonth;
151 ts.month = ts.month % 12;
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;
168 if( this->millisecond >= 1000 ){
169 this->millisecond = this->millisecond % 1000;
173 if( this->second >= 60 ){
174 this->second = this->second % 60;
178 if( this->minute >= 60 ){
179 this->minute = this->minute % 60;
183 if( this->hour >= 24 ){
184 this->hour = this->hour % 24;
188 unsigned int numDaysInMonth = 0;
189 if( this->month > 12 ){
190 numDaysInMonth = getNumDaysInMonth( this->month % 12 );
191 }
else numDaysInMonth = getNumDaysInMonth( this->month );
193 if( this->day >= numDaysInMonth ){
194 this->day = this->day - numDaysInMonth;
198 if( this->month > 12 ){
199 this->month = this->month % 12;
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;
216 if( millisecond < 0 ){
217 millisecond = this->millisecond + 1000 - rhs.millisecond;
222 second = this->second + 60 - rhs.second;
227 minute = this->minute + 60 - rhs.minute;
232 hour = this->hour + 24 - rhs.hour;
237 int numDaysInMonth = 0;
239 numDaysInMonth = getNumDaysInMonth( month - 1 );
240 }
else numDaysInMonth = getNumDaysInMonth( 12 - month );
242 day = numDaysInMonth - day;
258 ts.millisecond = millisecond;
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;
274 if( millisecond < 0 ){
275 millisecond = this->millisecond + 1000 - rhs.millisecond;
280 second = this->second + 60 - rhs.second;
285 minute = this->minute + 60 - rhs.minute;
290 hour = this->hour + 24 - rhs.hour;
295 int numDaysInMonth = 0;
297 numDaysInMonth = getNumDaysInMonth( month - 1 );
298 }
else numDaysInMonth = getNumDaysInMonth( 12 - month );
300 day = numDaysInMonth - day;
313 this->minute = minute;
314 this->second = second;
315 this->millisecond = millisecond;
320 unsigned long long getTimeInMilliseconds()
const{
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;
327 unsigned long long firstJan2014InMS = 1388534400000;
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;
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;
358 monthTime = marchInMs;
361 monthTime = aprilInMs;
367 monthTime = juneInMs;
370 monthTime = julyInMs;
386 return (yearTime + monthTime + dayTime + hourTime + minuteTime + secondTime + millisecond);
389 unsigned int getDayTimeInMilliseconds()
const{
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);
400 bool setTimeStampAsNow(){
401 #if defined(__GRT_OSX_BUILD__) || defined(__GRT_LINUX_BUILD__) 404 time_t tim = time(NULL);
405 tm *now = localtime( &tim );
407 if( now == NULL )
return false;
410 struct timeval nowTimeval;
411 gettimeofday( &nowTimeval, NULL );
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;
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;
435 warningLog <<
"setTimeStampAsNow() - Failed to get time stamp value! Unknown OS!" << std::endl;
439 bool setTimeStampFromString(
const std::string &timeString){
441 if( timeString ==
"NOW" || timeString ==
"now" ){
442 return setTimeStampAsNow();
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 );
452 }
else tempString += timeString[i];
455 if( tempString.size() > 0 ) s.push_back( tempString );
458 warningLog <<
"WARNING: Failed to set timestamp from string. Incorrect size! Size: " << s.size() << std::endl;
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] );
473 std::string getTimeStampAsString(
const bool includeDate =
true )
const {
474 std::string timeString =
"";
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(
"_");
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);
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()
522 timeString += grt_to_str(
"}");
526 std::string getTimeAsISOString()
const {
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");
545 unsigned int getNumDaysInMonth(
const unsigned int month ){
584 warningLog <<
"getNumDaysInMonth(const unsigned int month) - Bad month parameter: " << month << std::endl;
588 std::string pad(
const std::string &s)
const {
589 if( s.length() != 1 )
return s;
599 unsigned int millisecond;
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 'key ...