Skip to content

Instantly share code, notes, and snippets.

@SQReder
Created April 28, 2013 11:33
Show Gist options
  • Save SQReder/5476636 to your computer and use it in GitHub Desktop.
Save SQReder/5476636 to your computer and use it in GitHub Desktop.
#include <QTextStream>
#include <QDateTime>
QTextStream cout(stdout);
class TimeSpan {
qint64 _msecs;
static const qint64 c_second = 1000;
static const qint64 c_minute = 60;
static const qint64 c_hour = 60;
static const qint64 c_day = 24;
static const qint64 c_month = 30;
static const qint64 c_year = 12;
public:
TimeSpan(QDateTime begin, QDateTime end) {
_msecs = begin.msecsTo(end);
}
int GetSeconds() {
return _msecs / c_second % c_minute;
}
int GetMinutes() {
return _msecs / c_second / c_minute % c_hour;
}
int GetHours() {
return _msecs / c_second / c_minute / c_hour % c_day;
}
int GetDays() {
return _msecs / c_second / c_minute / c_hour / c_day % c_month;
}
int GetMonths() {
return _msecs / c_second / c_minute / c_hour / c_day / c_month % c_year;
}
int GetYears() {
return _msecs / c_second / c_minute / c_hour / c_day / c_month / c_year;
}
};
int main(int argc, char *argv[])
{
QDateTime dt = QDateTime::currentDateTimeUtc();
QDateTime dt1(QDate(2013,04,10), QTime(10,10,10));
QDateTime dt2(QDate(2014,04,9), QTime(10,10,10));
TimeSpan s(dt1, dt2);
cout << s.GetYears() << "/" << s.GetMonths() << "/" << s.GetDays() <<
" " << s.GetHours() << ":" << s.GetMinutes() << ":" << s.GetSeconds() << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment