Merge pull request #456 from ctrlaltca/gamesmodel_qt

Port #436 to QDateTime
This commit is contained in:
Gavin Bisesi 2014-11-26 17:28:35 -05:00
commit db0a77989b

View file

@ -2,8 +2,7 @@
#include "pb/serverinfo_game.pb.h" #include "pb/serverinfo_game.pb.h"
#include <QDebug> #include <QDebug>
#include <QStringList> #include <QStringList>
#include <sstream> #include <QDateTime>
#include <time.h>
namespace { namespace {
const unsigned SECS_PER_MIN = 60; const unsigned SECS_PER_MIN = 60;
@ -21,7 +20,7 @@ namespace {
* 91-300 minutes will return "Xhr ago" * 91-300 minutes will return "Xhr ago"
* 300+ minutes will return "5+ hr ago" * 300+ minutes will return "5+ hr ago"
*/ */
QString prettyPrintSecsAgo(uint32_t secs) { QString prettyPrintSecsAgo(unsigned int secs) {
if (secs < SECS_PER_MIN) { if (secs < SECS_PER_MIN) {
return QObject::tr("<1m ago"); return QObject::tr("<1m ago");
} }
@ -29,7 +28,7 @@ namespace {
return QObject::tr("<5m ago"); return QObject::tr("<5m ago");
} }
if (secs < SECS_PER_HOUR) { if (secs < SECS_PER_HOUR) {
uint32_t mins = secs / SECS_PER_MIN; unsigned int mins = secs / SECS_PER_MIN;
if (secs % SECS_PER_MIN >= 30) if (secs % SECS_PER_MIN >= 30)
mins++; mins++;
//: This will have a number prepended, like "10m ago" //: This will have a number prepended, like "10m ago"
@ -44,7 +43,7 @@ namespace {
// //
// Personally, I prefer to keep the code cleaner, and allow these. // Personally, I prefer to keep the code cleaner, and allow these.
if (secs < SECS_PER_MIN * 90) { if (secs < SECS_PER_MIN * 90) {
uint32_t mins = secs / SECS_PER_MIN - 60; unsigned int mins = secs / SECS_PER_MIN - 60;
if (secs % SECS_PER_MIN >= 30) if (secs % SECS_PER_MIN >= 30)
mins++; mins++;
return QObject::tr("1hr ") return QObject::tr("1hr ")
@ -53,7 +52,7 @@ namespace {
.append(QObject::tr("m ago")); .append(QObject::tr("m ago"));
} }
if (secs < SECS_PER_HOUR * 5) { if (secs < SECS_PER_HOUR * 5) {
uint32_t hours = secs / SECS_PER_HOUR; unsigned int hours = secs / SECS_PER_HOUR;
if (secs % SECS_PER_HOUR >= SECS_PER_MIN * 30) if (secs % SECS_PER_HOUR >= SECS_PER_MIN * 30)
hours++; hours++;
//: This will have a number prepended, like "2h ago" //: This will have a number prepended, like "2h ago"
@ -83,9 +82,9 @@ QVariant GamesModel::data(const QModelIndex &index, int role) const
switch (index.column()) { switch (index.column()) {
case 0: return rooms.value(g.room_id()); case 0: return rooms.value(g.room_id());
case 1: { case 1: {
uint32_t now = time(NULL); QDateTime then;
uint32_t then = g.start_time(); then.setTime_t(g.start_time());
int secs = now - then; unsigned int secs = then.secsTo(QDateTime::currentDateTime());
switch (role) { switch (role) {
case Qt::DisplayRole: return prettyPrintSecsAgo(secs); case Qt::DisplayRole: return prettyPrintSecsAgo(secs);