Okay, here's the final code snippet that answers my own question. I thought of sharing this in case it might helpful to some other people in the future. Thanks to Fred Larson for pointing the Boost example.
I chose Boost to do the DateTime calculation because my application already makes use of Boost somewhere else. I think I might have been able to use Qt as well, though I cant completely confirm.
Assuming DateTime is defined as:
class DateTime
{
public:
int year;
int month;
int day;
int hour;
int min;
int sec;
int millisec;
};
To do a simple DateTime comparison
bool DateTime::operator < (const DateTime& dt_)
{
using namespace boost::posix_time;
using namespace boost::gregorian;
ptime thisTime( date(this->year,this->month,this->day),
hours(this->hour) +
minutes(this->min) +
seconds(this->sec) +
boost::posix_time::millisec(int(this->millisec)) );
ptime thatTime( date(dt_.year,dt_.month,dt_.day),
hours(dt_.hour) +
minutes(dt_.min) +
seconds(dt_.sec) +
boost::posix_time::millisec(int(dt_.millisec)) );
return thisTime < thatTime;
}
To add 2 DateTime together to return a new DateTime
DateTime DateTime::operator + ( const DateTime& dt_ )
{
using namespace boost::posix_time;
using namespace boost::gregorian;
date thisDate( this->year, this->month, this->day );
date newDate = thisDate + years(dt_.year) + months(dt_.month) + days(dt_.day);
ptime newDateTime( newDate,
hours(this->hour) + hours(dt_.hour) +
minutes(this->min) + minutes(dt_.min) +
seconds(this->sec) + seconds(dt_.sec) +
boost::posix_time::millisec(int(this->millisec)) +
boost::posix_time::millisec(int(dt_.millisec))
);
DateTime dateTime;
date t1_date = newDateTime.date();
dateTime.year = t1_date.year();
dateTime.month = t1_date.month();
dateTime.day = t1_date.day();
time_duration t1_time = newDateTime.time_of_day();
dateTime.hour = t1_time.hours();
dateTime.min = t1_time.minutes();
dateTime.sec = t1_time.seconds();
dateTime.millisec = t1_time.fractional_seconds()/1000.0f;
return dateTime;
}